4 ms·
Polymorphic this Typing seems like a major change with many possible corner cases. I think Dart .. operator is a better solution newModel.setupBase().setup
by lukasm 11y ago
Polymorphic this Typing seems like a major change with many possible corner cases. I think Dart .. operator is a better solution
newModel.setupBase().setupAdvanced();
becomes
newModel..setupBase()..setupAdvanced();
- Arnavion 11y agoThat assumes setupBase(), etc are either void-returning or return the this-instance (newModel). It won't work if each method returns a new instance, such as methods of immutable collections. Edit: Since this is hard to google for, this is called the "cascade operator". The given example is equivalent to: newModel.setupBase(); newModel.setupAdvanced(); That is, it ignores the result of the method and runs the remaining part of the statement with the LHS of the operator.
- mythz 11y ago> That assumes setupBase(), etc are either void-returning or return the this-instance (newModel). This is Incorrect, it doesn't assume anything. Dart's method cascades just returns the receiver, it doesn't matter what the method returns, it's value is ignored and receiver is returned instead. http://news.dartlang.org/2012/02/method-cascades-in-dart-posted-by-gilad.html http://news.dartlang.org/2012/02/method-cascades-in-dart-pos... Since the return value is ignored, it's only useful for methods with side-effects.
- spion 11y agoIt assumes you have a stateful, side-effectful API when you could instead have an API that returns new immutable objects of the same type
- spankalee 11y agoIn that case instead of cascades you just use regular invocation.
- Arnavion 11y ago>This is Incorrect, it doesn't assume anything. Dart's method cascades just returns the receiver, it doesn't matter what the method returns, it's value is ignored and receiver is returned instead. ... which would only be useful if the method was void-returning or returned the this-instance, i.e., it didn't return a new instance and left the LHS unmodified.
- deleted 11y ago[deleted]
- deleted 11y ago[deleted]
- mythz 11y ago> which would only be useful if the method was void-returning or returned the this-instance Simply untrue, there are plenty of times methods have side-effects that return something other than void or itself, e.g: - Incrementing a value and returning the current value - Inserting or Updating rows and returning the number of rows affected - Setting an entry and returning whether there was an existing entry or not
- Arnavion 11y agoThe example I was responding to was about "setupBase" and "setupAdvanced"...
- sjrd 11y ago> It won't work if each method returns a new instance, such as methods of immutable collections. TS' this type does not allow that either. You have to return `this`, and nothing else. This can be explained by inheritance. If you extend a class with a method returning a `this` type, but not returning `this`, somehow you should be forced to override that method. But you probably don't want that. Also, the `this` type introduced by TS is basically equivalent to Scala's `this.type`.
- Arnavion 11y agoD'oh! You're right of course.