-
Notifications
You must be signed in to change notification settings - Fork 207
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
Add private set property modifier #4206
Comments
I've suggested something like that before. Can't find it right now. Strawman syntax: int _value {
get value;
set;
}; This block states that there is a getter named By allowing you to specify an alternative name for a getter or setter, you can end up with default getters and setters for a variable with different names, including one being private. Another way to write the same thing might be int value { set _value; }; because you can't omit a getter, so omitting it is equivalent to (Annotations are not an option. An annotation cannot change language behavior, so |
I like this sintax, for me it works int a { set _a; } What if I set an initial value for 'a'? It would be something like this? int a = 1 { set _a; } |
For each verb in {
Each of those has to be translated into the human language while reading the program (16 combinations in total). |
Please consider a different syntax (C#-inspired) int _a via a { get; set; } // "via" or something to the same effect which is a shorthand for int _a;
int get a => _a;
set a(int value) => _a = value; In C#, there's also an "init" setter to be used in the constructor; in dart, we can use its variant to provide a default value for the constructor parameter, e.g. final int x = 0; // looks bad. What does it mean? We want to set it in a constructor through an optional parameter.
final int x { init = 0; } // better, the default for an optional parameter is 0 |
This syntax is C++ inspired. int _a {get a; set a;} reads better. And it allowed the getter and setter to have different names if you want it. It does mean you have to say the name twice, but only if both are different from the storage name, and unlike C# you don't need to have a private storage to support public getters and setters, so you can always make one of them the same name as the field. The above could just be |
int _a {get a;} doesn't tell you there's no setter called "a". There can be an old-style setter |
One interesting possibility is that if something is declared with the syntax |
Introduce a property-level modifier similar to Swift’s
private(set)
functionality. This would allow a class to expose a property for reading externally while restricting write access.Motivation
In Swift, a common pattern is to declare properties as
public private(set)
, which makes them publicly readable but only privately (or internally) writable. However, Dart does not have a similar feature, so we have to create a private variable along with a getter to achieve the same functionality.Swift
Example:Dart
Example:Proposed Solution
_set
@nonVisibleSet
Alternatively, a annotation-based could be another option.
The text was updated successfully, but these errors were encountered: