Can Dart Overload Assignment Operator?

I have the following class:

class EventableNumber{

  num _val;

  num get val => _val;

  void set val(num v){

    num oldValue = _val;
    _val = v;
    _controller.add(new NumberChangedEvent(oldValue, v));

  }

  StreamController<NumberChangedEvent> _controller = new StreamController<NumberChangedEvent>();
  Stream<NumberChangedEvent> _stream;
  Stream<NumberChangedEvent> get onChange => (_stream != null) ? _stream : _stream = _controller.stream.asBroadcastStream();

  EventableNumber([num this._val = 0]);

}

Is it possible to overload the assignment operator =? instead of using valgetter and setter to force the event to fire when the value changes, it would be nice if it could be done when the user writes myEventableNum = 34, and then myEventableNumhis event first onChange, but rather than myEventableNum.val = 34.

+4
source share
1 answer

Dart does not allow this.

However, did you consider function style calling?

Basically, if you define a function called a "call" in the EventableNumber class, you can call the instance as a function:

myEventableNum(34)

, Function:

class EventableNumber implements Function {
...
  void call(val) {...}
...
}

, :)

+6

All Articles