Skip to content

Define default "get" semantics #87

@DartBot

Description

@DartBot

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();
  }
}

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-not-plannedClosed as we don't intend to take action on the reported issuetype-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions