-
Notifications
You must be signed in to change notification settings - Fork 214
Feature request: getters that return an immutable value #3330
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
Comments
Cf. #1518, quite similar. |
I don't understand what the request is, can you be more precise? The code you write there behaves precisely as you describe now. Where do you want a compile error? On the definition of the |
Sorry for not being clear, I will try to re-explain. |
Why does this not do what you want: abstract interface class ReadableMyValue {
int get value;
}
class MyValue implements ReadableMyValue {
int _value;
MyValue(this._value);
int get value => _value;
void increment() {
_value++;
}
}
class MyClass {
MyValue _myValue = MyValue(42);
ReadableMyValue get myValue => _myValue;
}
void main() {
final myObject = MyClass();
print(myObject.myValue.value); // Access the value through the getter, prints: 42
myObject.myValue.increment(); // Static error here
print(myObject.myValue.value); // Access the modified value, prints: 43
} |
@leafpetersen This is doing exactly what I want. I didn’t know you could do that, thanks 🙏. |
Yep. It's a big lift to figure out what the semantics of that should be. In the meantime, read only interfaces get you a fair distance. |
Uh oh!
There was an error while loading. Please reload this page.
I would be able to have getters that return an immutable value and create a compile error with the following code
Here is a possible syntax:
The text was updated successfully, but these errors were encountered: