-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
This issue was originally filed by @seaneagan
One very common use case for setters is to validate field values. Currently to do this, one must create a companion private field, set this field in the public setter if validation passes, and define a public getter which returns the private field like so:
class A {
num _x = 0;
num get x() => _x;
set x(num v) {
if(v > 5) throw new Exception();
_x = v;
}
}
This could be shortened drastically by specifying that if an accessor only defines a "set" handler, then anytime the "set" completes without throwing an exception, the passed value of the field is stored, and returned for subsequent "get"s. Then the above could be shortened to just:
class A {
set x(num v) {
if(v > 5) throw new Exception();
}
}