Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const docstring gives over-eager advice #57565

Closed
adienes opened this issue Feb 27, 2025 · 3 comments
Closed

const docstring gives over-eager advice #57565

adienes opened this issue Feb 27, 2025 · 3 comments
Labels
docs This change adds or pertains to documentation

Comments

@adienes
Copy link
Member

adienes commented Feb 27, 2025

the docstring:

const is used to declare global variables whose values will not change. In almost all code (and particularly performance sensitive code) global variables should be declared constant in this way.

it may seem a bit too-obvious but I think the docs should emphasize more strongly here that a variable should only be declared const if it is actually constant. the wording "almost all code" is quite strong here and there might also want to be information here that it is not just about the value changing but about the non-reassignment

that is, I doubt this is a legal use of const even though the value never changes (...unless it is in fact legal?)

const x = 1

function foo()
    while rand() < 0.99
        global x = (rand(1:2:11)^2) % 4
        println(x)
    end
end
@adienes adienes added the docs This change adds or pertains to documentation label Feb 27, 2025
@adienes
Copy link
Member Author

adienes commented Feb 27, 2025

also fun fact this segfaults on nightly but not 1.11

@Keno
Copy link
Member

Keno commented Feb 28, 2025

Well, I think it is very much true that the mutable globals have limited utility in library code. In almost all cases you need something more specific like a thread or task local mutable global. I think the primary use case for mutable globals is the REPL.

@adienes
Copy link
Member Author

adienes commented Feb 28, 2025

fair. maybe "that will not be reassigned" is marginally better than "whose values will not change," but either way I suppose it gets the same message across.

@adienes adienes closed this as completed Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

2 participants