3 ms·
You can get something similar via method chaining: x.something().somethingElse().anotherThing() C# extension methods aren't the perfect solution for this, but
by ziml77 5y ago
You can get something similar via method chaining: x.something().somethingElse().anotherThing()
C# extension methods aren't the perfect solution for this, but I love that they exist to at least make chaining possible without needing to modify the class that I'm applying the function to. D has an even better version of this called UFCS which makes it so `Bar something(Foo f)` can either be called as `something(x)` or `x.something()` without needing any special annotations like C# requires.
- substation13 5y agoIn F# (the most widely used ML?) expressions can be much more complicated than that, incorporating loops, conditionals etc. To illustrate: let x = let mutable maxScore = 0 for item in items do if item.Score > maxScore then maxScore <- item.Score maxScore Compared to: const x = (() => { let maxScore = 0; for (const item of items) { if (item.score > maxScore) { maxScore = item.Score; } } return maxScore; })(); Not necessarily the best way to write this (you would probably use a sequence library) but hopefully conveys the idea. Extension methods are definitely useful in OOP languages. However, a much cleaner language design is to remove the need for a "class" altogether and just have free-functions, function composition and a pipe operator. None of this is something you can't simulate in OOP / procedural languages, it's just much more clunky.