UFCS and overloading
cym13 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 7 07:45:18 PDT 2015
On Monday, 6 April 2015 at 18:00:46 UTC, Szymon Gatner wrote:
> Why is that? The use case is to provide a set of convenience
> "extension methods" to a basic interface. Say, given:
This is not the only use case, another (maybe even more common)
use is to allow pipeline programming.
Example from one of Andrei's talks (
http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/D ):
import std.algorithm, std.stdio, std.range, std.conv;
void main()
{
stdin
.byLine
.filter!(s => !s.empty && s.front != '#’) // Filter with
this lambda function
.map!(s => s.to!double) // Map
the strings to doubles
.array
// Sorting needs random access
.sort!((a, b) => a < b) //
Another lambda
.take(10)
// Applyable to any range
.writeln;
}
With such constructs, it is important to be able to call a method
and not the function as there are ways to explicitely call the
function but not the method (AFAIK). As this style of programming
is actively encouraged by both Andrei and Walter, I think the
design is coherent.
More information about the Digitalmars-d-learn
mailing list