question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Dart's cascade/chaining operator `..`

See original GitHub issue

Here’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:closed
  • Created 7 years ago
  • Comments:19 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
ncoraicommented, Oct 26, 2016

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:

  • the object is immutable and returns a copy with the state altered, or
  • the API is fluent and the same instance is returned in an altered state.

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.

0reactions
ncoraicommented, Oct 29, 2016

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 the with operator discussion to its own issue if need be.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found