IssueRaiser

Add support for immutable/one-time assignable variables

**Describe the project you are working on:** video game **Describe the problem or limitation you are having in your project:** I can't enforce a varriable only being assigned once **Describe the feature / enhancement and how it helps to overcome the problem or limitation:** I propose adding a new keyword to GDScript: `val` in addition to the existing `var` for declaring variables. This would force the variable to only be assigned once. Kotlin, ECMA6, and Swift all have a feature similar to this. **Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:** declaring a variable as `val` will throw a error if the variable is assigned more than once. It allows for a few nice conventions: 1) It allows for a sort of scoped constant (currently constants can only be global) 2) It prevents accidental re-assignments for varriables that weren't intended to change. It makes maintaining the code less error prone. 3) It can enforce a variable only gets assigned once in an if/else chain or match statement: ```gdscript # This is valid val x: int if foo > 2: x = 42 else: x = -5 ``` ```gdscript # This is valid val x := 42 ``` ```gdscript # This is NOT valid val x: int = 42 x = -5 ``` **If this enhancement will not be used often, can it be worked around with a few lines of script?:** Can't be worked around **Is there a reason why this should be core and not an add-on in the asset library?:** Can't, it's a language level feature

Posted by Wavesonics on May 10, 2020, 10:12 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments

support (Other)

Add support for immutable/one-time assignable variables

WavesonicsMay 10, 2020, 10:12 PM

**Describe the project you are working on:** video game **Describe the problem or limitation you are having in your project:** I can't enforce a varriable only being assigned once **Describe the feature / enhancement and how it helps to overcome the problem or limitation:** I propose adding a new keyword to GDScript: `val` in addition to the existing `var` for declaring variables. This would force the variable to only be assigned once. Kotlin, ECMA6, and Swift all have a feature similar to this. **Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:** declaring a variable as `val` will throw a error if the variable is assigned more than once. It allows for a few nice conventions: 1) It allows for a sort of scoped constant (currently constants can only be global) 2) It prevents accidental re-assignments for varriables that weren't intended to change. It makes maintaining the code less error prone. 3) It can enforce a variable only gets assigned once in an if/else chain or match statement: ```gdscript # This is valid val x: int if foo > 2: x = 42 else: x = -5 ``` ```gdscript # This is valid val x := 42 ``` ```gdscript # This is NOT valid val x: int = 42 x = -5 ``` **If this enhancement will not be used often, can it be worked around with a few lines of script?:** Can't be worked around **Is there a reason why this should be core and not an add-on in the asset library?:** Can't, it's a language level feature

Comments (0)

No comments yet. Start the discussion.

Comment on this issue