Add Dart's cascade/chaining operator `..`
See original GitHub issueHere’s a request that is based on #4049 but with a simpler scope that eschews the complexity from that request: add a ..
operator that desugars to reusing the instance at the start of the chain.
value myLongValueName =
construction()
..descriptivelyNamedMethodFoo(1, 2)
..otherMethodBar{ myFirstParam = aValue; thisIsDifferent = somethingElse; }
..heresAnotherBaz();
would desugar to
value myLongValueName = construction();
myLongValueName.descriptivelyNamedMethodFoo(1, 2);
myLongValueName.otherMethodBar{ myFirstParam = aValue; thisIsDifferent = somethingElse; };
myLongValueName.heresAnotherBaz();
It can be argued that the benefit of readability afforded by fluency isn’t worth tailoring APIs for it, especially as Ceylon isn’t leaning toward it in the first place, but surely a simple ..
operator with a well-specified and understandable behavior could do the trick, even if it’s not associated with an interface (I reckon that Gavin has changed his mind on more fundamental tenets of Ceylon than code sugar rules).
Issue Analytics
- State:
- Created 7 years ago
- Comments:19 (14 by maintainers)
Top Results From Across the Web
Method chaining using Cascade in Dart - FlutterMaster
Method chaining is a common design pattern in object-oriented programming, ... Google natively supports method chaining through Cascade in Dart?
Read more >Method Cascades in Dart - Dart News & Updates
The add() method can do its usual thing and return its arguments. However, you can get a chaining effect using cascades: myTokenTable.
Read more >How do method cascades work exactly in dart? - Stack Overflow
The cascade operator in Dart makes this requirement unnecessary. Methods can still be chained even though they don't return the current object.
Read more >Proposal: Method Cascading, or the .. operator #8947 - GitHub
With cascades, no one needs to plan ahead or make this sort of tradeoff. The Add() method can do its usual thing and...
Read more >Flutter Cascade Operator (..) - YouTube
How to use the Cascade Operator with two dots (..) in Flutter and Dart to make a sequence of operations on the same...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Its value is that it solves a design conundrum: currently, a method that returns the instance on which it is invoked can indicate two things:
This dichotomous approach muddles the clarity of an API, and forces a choice on the designer. The cascade operator resolves that ambiguity, since you don’t need fluent APIs anymore. Fluency becomes a choice for the invoker, provided by the compiler for free.
I should note that there are other use cases for fluent APIs than Builder patterns. Recently, I was confronted with IO streams that would force me to repeatedly write the variable name, needlessly cluttering the method body.
Because of the overlap with the range operator, I don’t feel 100% comfortable with adding this I’ll admit, so I won’t advocate it any further. As I realized after opening the issue, it would substitute the
..
operator ambiguity to the return value ambiguity, so not a clear win. I’ll close this issue and you guys can move thewith
operator discussion to its own issue if need be.