-
-
Notifications
You must be signed in to change notification settings - Fork 661
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
Private getters/setters #3053
Comments
We cannot change the visibility interpretation just like this because it would break a lot of code. This should be a rare use-case for which it is fair to require you to manually call an accessor method. |
How can it break existing code if there is no private_set? |
@Simn Sorry for not making my point clear. I didn"t mean to change the current visibility, instead I suggested to add a new accessor. |
|
public var myInt(get, never):Int
private var myInternalInt(default, set):Int
private function get_myInt():Int return myInternalInt;
private function set_myInternalInt(v:Int):Int
{
//do something
return myInternalInt = v;
} Now I have to do this, when outside the class I access To me, I think it is somewhat inconsistent and redundant to access different variables at different scopes. @nadako not sure about the difficulty to implement this, I thought it is just a combination of |
I"m pretty sure this can be done with a macro :) |
Just a reminder that haxe still lacks complete accessor combinations
|
I"m stumbling upon this issue from time to time and it is pretty annoying that we don"t have "private set".
Normally I would agree, but this problem often arises when using macros to build all kinds of "change-tracking" data classes. For example, consider we have something like: class Player implements Magic /* @:autoBuild here */ {
public var health:Int;
public function damage(amount) {
health -= amount;
}
} what class Player {
public var health(default,set):Int;
public function damage(amount) {
health -= amount;
}
function set_health(value) {
this.health = value;
dispatchChange(); // or markDirty or whatever
return value;
}
} So far so good, but imagine user wants to forbid writing to
Just because there"s no accessor-method counterpart to
Can we please reconsider in view of the problem I described above? |
FWIW this must be an issue for hxbit as well. If not, I"m very curious how it is solved. |
I agree we need a solution for this. |
I"m very much in favor, although this seems like a bad time to start deprecating accessors. Since the solution is primarily intended for macros, perhaps a solution based on field metadata would be an adequate path to support this without breaking changes. |
I"ve faced this issue out of a macro context a few times. Being forced to introduce additional fields doesn"t feel good. |
Could we get |
While I think we should change Also, I"m not sure if it should be |
Sometimes we need a getter or setter, but only privately.
Example:
The text was updated successfully, but these errors were encountered: