problem with byLine

Ali Çehreli acehreli at yahoo.com
Tue May 15 12:49:30 PDT 2012


On 05/15/2012 11:54 AM, dcoder wrote:
 > On Tuesday, 15 May 2012 at 16:03:16 UTC, Matt Soucy wrote:
 >>> I'm trying to learn D, by playing with code and reading this forum. I'm
 >>> a slow learner. :)
 >>>
 >>> Anyways, I looked at std.stdio code and noticed that byLine resturns a
 >>> struct ByLine, but where does the .map come from? Thanks!
 >>>
 >>
 >> It comes from std.algorithm. What that line does is:
 >> f.byLine() // Get by lines, exactly as you know already
 >> .map!"a.idup"() // Iterate over the byLine, and make a Range of
 >> immutable strings with the same contents as each line.
 >> .array() // Convert it from a range to an array of strings
 >>
 >> This is achieved through templates accepting strings at compile time
 >> to be somewhat like lambda functions, and UFCS.
 >
 >
 >
 > 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.
 >

Matt has already answered that: UFCS. :)

Very briefly, UFCS makes the compiler apply this logic: "if such a 
member function does not exist, then look for a regular function that 
has the same name and that can take the object as its first parameter."

For example, let's say that you have the following type and the regular 
function:

   struct S
   {}

   void foo(S s, int i, double d)
   {
       // ...
   }

Then, if there is this call in your code,

   S s;
   s.foo(42, 1.5);

Since S does not have a foo() member function, the compiler then tries 
the following call and compiles it happily:

   foo(s, 42, 1.5);

Ali

-- 
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html



More information about the Digitalmars-d-learn mailing list