problem with byLine
Chris Cain
clcain at uncg.edu
Tue May 15 12:57:45 PDT 2012
On Tuesday, 15 May 2012 at 18:54:51 UTC, dcoder wrote:
> Okay thanks alot Matt and Ali, that helps alot, but I still
> don't
> get the dot in ".map" from the line above. Doesn't the dot mean
> that .map is a member or a member function of the return value
> of
> byLine()? Which, in this case is struct ByLine?
>
> thanks.
A newer feature of D, UFCS (or Uniform Function Call Syntax)
makes it so that you can use the dot syntax to mean either a
member or member function OR it rewrites the call using the thing
before the dot as the first parameter.
Let me show you what I mean:
struct S {
int memberFunc() { return 0; }
}
int normFunc(S firstParam) { return 1; }
S s;
s.memberFunc(); // Calls the member function in S
s.normFunc(); // Rewritten to normFunc(s);
So in the examples they gave above, this is actually what is
executed:
f.byLine().map!"a.idup"().array();
array(map!"a.idup"(f.byLine()));
So, as you can see f has the byLine member function, but the
ByLine struct does not have a map function, so it's rewritten
like that. Also, the Range returned by map does not have an array
function, so it's rewritten again as the first parameter. As you
can see, it would normally have to be read backwards (and with a
bunch of parentheses to match), which can be confusing... while:
f.byLine().map!"a.idup"().array();
Is more natural to read (by most) ... you're taking the lines,
using the a.idup function on each, and then making an array from
those elements.
More information about the Digitalmars-d-learn
mailing list