-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Suggestion: readonly constructor parameters #7590
Comments
So would a |
No, it would simply be a shorthand for creating properties in the manner of |
This can probably be implemented using a decorator on the function doing a defensive copy on the arguments. However, that would be a runtime check. |
@leonyu Copying the arguments is not what I'm after. I'm asking for exactly the same semantics as |
+1 |
Makes a lot of sense. We'll include this along with the first release with |
Awesome, thanks! |
Fix #7590: Allow 'readonly' to be used in constructor parameters
Really glad this is implemented. In addition to the increased expressiveness, this allows a fair amount of code currently using accessibility modifiers for the sake of brevity to be written with clearer intent. |
I've been using the new
readonly
properties feature (#6532) and have found it to be a very productive alternative to using getters orgetWhatever()
functions, or as is often the case, simply doing nothing to enforce read-only properties. However I'm really yearning for the ability to mark constructor parameters asreadonly
.In 1.8 a common pattern for me is to write classes where all properties are public and readonly like so:
But now with
readonly
I'm missing out unless I rewrite the above code to:Which is pretty cumbersome. What I'd really like to be able to write is:
Right now I'm worried about hitting 2.0 and having to rewrite all my constructors just to take advantage of
readonly
. Because it's an advantage I definitely want to have.The text was updated successfully, but these errors were encountered: