Skip to content

const docstring gives over-eager advice #57565

@adienes

Description

@adienes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis change adds or pertains to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions