4 ms·
I like dart's cascade operator (..) a lot, that way you can chain calls on an object without the functions returning this. I'm sure other languages have somethi
by BoardsOfCanada 5y ago
I like dart's cascade operator (..) a lot, that way you can chain calls on an object without the functions returning this. I'm sure other languages have something similar, kotlin has apply which feels a bit more heavy handed though.
- donatj 5y agoOooh I didn’t know this was a thing. I had that as an idea recently but it’s really interesting to hear it already exists! I have been hearing a bunch of interesting things about Dart lately and feel like I should check it out.
- deleted 5y ago[deleted]
- mpweiher 5y agoPretty sure this was taken from Smalltalk, which uses the semicolon as the cascade. So a b c d. sends c to the result of sending b to a, and d to the the previous result etc. a b; c; d. Sends b, c, and d to a. This is often indicated visually: a b; c; d.
- musicale 5y agoSemicolon/method cascade is one of my favorite bits of Smalltalk syntax. I'm a bit neutral on returning self (either explicitly or implicitly), although it enables left-to-right pseudo-expressions like 1 + 2 * 3.
- mpweiher 5y agoI am also fond. And interestingly, the cascade actually separates keyword messages nicely without having to use brackets, which normal messaging does not. So: a foo:2; bar:4. This sends foo: and bar: to a. But you can't do that without the cascade: a foo:2 bar:4. That's not bar: sent to the result of a foo:, that's foo:bar: sent to a. For normal messages you have to use brackets: (a foo:2) bar:4. Which is ugly and asymmetrical with respect to cascades. So in Objective-S, I introduced the pipe for message chaining: a foo:2 | bar: 4. IMHO, this works both visually and semantically, because you are "piping" the result of "a foo:2" into the message expression " bar: 4", as the receiver.
- ptx 5y agoWhat's heavy-handed about Kotlin's apply? It's just an extension function in the standard library (so not a separate language feature, if that's what you mean by "heavy"?) and solves the problem cleanly IMHO. You can easily implement a similar extension function in C#, although the syntax is slightly uglier and you don't get the implicit receiver in the lambda.
- BoardsOfCanada 5y agoI just mean it looks a bit more obtrusive to me than just .. I agree that it speaks volumes about the power of the language itself.
- Someone 5y agoPascal (https://www.freepascal.org/docs-html/ref/refsu62.html https://www.freepascal.org/docs-html/ref/refsu62.html) and Visual Basic (https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement https://docs.microsoft.com/en-us/dotnet/visual-basic/languag...) have with. That serves a similar purpose, (in VB only for accessing multiple fields in an object, I think) Visual Basic’s syntax is a bit safer. Requiring that seemingly superfluous dot means there can’t be confusion between referencing fields and referencing local or global variables. In Pascal adding a field to a record whose name shadows that of a local can change the meaning of the statements in a with statement, and removing it may silently ‘fix’ a broken reference to a no longer existing field. with A, B, C, D do is asking for trouble even more.