array typed as 'const' is not constant?

thekthek Posts: 2Member

Is it by design that an array typed as 'const' is alterable? That seems to contradict the purpose of a constant.

const a=0 can't be altered. This makes sense.
But const a=[0] can be. (e.g. in the _process function using: a[0] +=1)

One can also append values to a. Is this by design?

Comments

  • MegalomaniakMegalomaniak Posts: 2,654Admin

    Isn't the constant to an array just a reference. The array itself would still be a variable but the constant is a reference pointing to the array and it's that reference that shouldn't change since it is a constant? At least that's what I'd expect.

  • TwistedTwiglegTwistedTwigleg Posts: 2,666Admin

    I think it is like @Megalomaniak explained, that in GDScript arrays are just pointers/references. So the constant would just be a constant reference/pointer to an array, rather than a constant array itself. This note in the documentation suggests this is what is happening:

    Note: Arrays are always passed by reference. To get a copy of an array which can be modified independently of the original array, use duplicate.

    To be fair, it may be worth opening an issue on the documentation repository to clarify this. Even adding something like

    Arrays marked const in GDScript will give a constant pointer to the given array, rather than making the array itself a constant.

    Would help clear up confusion on how arrays in GDScript work when marked constant.

  • thekthek Posts: 2Member

    Good insights.

    Isn't that backwards from the concept of a const? I mean, we don't use const for variables and then expect them not to change.

    That would be a great addition to the documentation, and perhaps a bit how to use constants recursively and appropriately. Right now, much of the dynamic use of GDscript is too esoteric, and I'd be glad to help make it more exoteric. But there's much that's unclear conceptually.

    To this end, the follow-up question perhaps is: what's the benefit of passing arrays only by reference? Is there a proper use for this? Otherwise, how is one supposed to use a const set of array-data recursively? The trouble is that if it constantly reassigns data in the original, then is it supposed to only be used like a template? And thus users must be careful not to inadvertently change their 'original' template data?

    One can duplicate it, and perhaps that's the proper way. But a modifiable template seems different than both a const or a var. Maybe I'm missing something here.

    Similarly, what use is a constant reference to an array? What's the use-case of that? Aren't references always constant in a language? I believe the original declaration always captures the reference? I've never had a case where my variable name referenced something different than it's own contents.

    It seems much more straightforward to me (although I've only been using gdScript for a few months) to use a single-standard for constants (i.e. that right-hand assignations are immutable when typed 'const'), rather than a double-standard, where arrays and enums are 'exempt' while single declarations aren't. Certainly the engine is doing something different than I'd expect, but what might that be?

  • TwistedTwiglegTwistedTwigleg Posts: 2,666Admin
    edited June 25

    @thek said:
    To this end, the follow-up question perhaps is: what's the benefit of passing arrays only by reference? Is there a proper use for this? Otherwise, how is one supposed to use a const set of array-data recursively? The trouble is that if it constantly reassigns data in the original, then is it supposed to only be used like a template? And thus users must be careful not to inadvertently change their 'original' template data?

    I'll be honest: I have also wondered why Godot has arrays passed by reference by default, while most every other data type does not follow this convention. I have assumed it was a data limitation or structure choice, but I never have really looked into it.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file