Can I pass a setter (not a value) as a parameter to a function without reflection?

I want to pass the class field installer as a parameter to a function so that the function can perform the assignment.

Is there a way without using reflection?

+4
source share
1 answer

You cannot directly pass the installer.

To avoid reflection, you can wrap the setter inside a function:

class A {
  String _attr=;
  set attr(String v) => _attr = v;
}

main() {
  final a = new A();

  // create a wrapper function to set attr
  final setter = (v) => a.attr = v;

  callSetter(setter);
  print(a._attr);
}

callSetter(setterFunction(value)) {
  setterFunction("value");
}

This proposal for generalized passages is approved and is likely to be implemented in the near future and will allow to fix getters and setters, for example:

var setter = a#attr;
// and can be invoked like
setter(value)
+5
source

Source: https://habr.com/ru/post/1530185/


All Articles