Proposal: readonly local variables + Explicit 'mutable' specifier to help with code analysis + Related compiler settings #3746
Replies: 4 comments
-
That sounds like a duplicate of #188.
At least for fields, that sounds like something that could be achieved using an attribute. |
Beta Was this translation helpful? Give feedback.
-
Thanks @svick for noting that my proposal includes something that was already suggested. Still, I think it makes the most sense in combination with the |
Beta Was this translation helpful? Give feedback.
-
There's a proposal for that too: #2478. Though it hasn't seen any movement in quite some time. |
Beta Was this translation helpful? Give feedback.
-
Wow. #188. That issue was championed 3 ½ years ago! I know about the prioritisation process (roughly) for features but, damn, I thought this one would have been chosen for development by now. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, fields can be assigned and re-assigned by default, unless an explicit
readonly
modifier is used.readonly
fields exist to help the programmer ensure certain invariants during the lifetime of an object.My first proposal is to provide a similar functionality for local variables. They would have to be assigned a value during declaration and this would ensure certain invariants during a function call. Something like this:
My second proposal is to create a new
mutable
modifier for fields and variables, which would allow us to explicitly specify the semantics that fields now have by default (when thereadonly
modifier is not present). This would help code analysis. Someone could configure their compiler such that fields and variables without any specifier are treated asreadonly
by default, or at least give info/warnings/errors when a non-mutable
field or variable is re-assigned to, just like we can currently configure the compiler to allow for non-nullable reference types to exist.Edit: It would also be a good idea to have readonly parameters, as proposed here. Thanks, @svick
Beta Was this translation helpful? Give feedback.
All reactions