You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My use case is the following (illustrated via a contrived example):
classPoint {
// Note these variables are privatefinaldouble _x;
finaldouble _y;
// Note that I am using named variables.Point({first, second}) {
this._x = first;
this._y = second;
}
}
That is, I have two private variables that I would like to keep final as well (because of all the benefits of immutable references).
I would like to offer a named constructor for my class as shown above. However, I have not found this to be possible. (Perhaps I am missing something?)
This is possible in Swift, for example:
structPoint{privateletx:Double;
privatelety:Double;
init(first:Double, second:Double){
x = first
y = second
}}
Basically, I would like to work with my class in the original way I'd imagined (private and final members), and also expose a nice initialisation API to consumers.
The text was updated successfully, but these errors were encountered:
We don't allow you to have named parameters using private names. It would make that parameter unspeakable from other libraries. (We could probably allow it in private methods, but decided that it's just not that useful, so the effort needed to make an exception was not worth its own weight. It's only really constructors with initialing formals where it matters, because you want to write this._x, and you'll just have to write a normal parameter and an initializer list entry there).
The late final double _x introduces a setter to the interface. Since it's a private name, that won't affect users of the class, but it is an unnecessary overhead. Using late is like using dynamic — occasionally necessary, but something which should be avoided when it's not necessary, and when used, it should be well encapsulated so your class's clients won't see it.
Uh oh!
There was an error while loading. Please reload this page.
My use case is the following (illustrated via a contrived example):
That is, I have two private variables that I would like to keep
final
as well (because of all the benefits of immutable references).I would like to offer a named constructor for my class as shown above. However, I have not found this to be possible. (Perhaps I am missing something?)
This is possible in Swift, for example:
I have found a workaround however:
Basically, I would like to work with my class in the original way I'd imagined (private and final members), and also expose a nice initialisation API to consumers.
The text was updated successfully, but these errors were encountered: