dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.57k forks source link

Reduce boilerplate to hide setters #24054

Open BlackHC opened 9 years ago

BlackHC commented 9 years ago

We have lots of boilerplate in AWV of the form:

T _field;
T get field => _field;

It would be nice if there was a way to only make the setter of field private.

class X {
  readonly int field;

  void someMethod() {
    field = 3; // allowed;
  } 
}
... different package:
void main() {
  new X().field = 4; // disallowed, field= is package private;
}
sethladd commented 9 years ago

reminds me of Rails, where you could use attr_accessor :field(adds both a getter and setter) or attr_reader :field (just the getter) or attr_writer :field (just the setter)

In any case, please email core-dev@dartlang.org mailing list (you might have to join the list first) with this proposal. We have a formal DEP process, detailed here: https://github.com/dart-lang/dart_enhancement_proposals

vanlooverenkoen commented 5 years ago

In Kotlin it is done this way:

var setterVisibility: String = "abc"
    private set

In a project I am working on now we have a half a file of boilerplate code to make public getters and private setters. 😑