How does laziness and UFCS interact?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 10 10:42:35 PDT 2015


On 03/10/2015 08:00 AM, John Colvin wrote:

 > On Tuesday, 10 March 2015 at 14:41:00 UTC, Logan Capaldo wrote:
 >> On Monday, 9 March 2015 at 22:15:43 UTC, Ali Çehreli wrote:
 >>> You are right. I had the same observation at minute 11:27 below,
 >>> where I warn against UFCS with assumeWontThrow:
 >>>
 >>>
 >>> 
http://www.youtube.com/watch?feature=player_detailpage&v=oF8K4-bieaw#t=687
 >>>
 >>>
 >>> Ali
 >>
 >> Sorry, which is right?

You are right that it is confusing. :)

 >> I know ifThrown is lazy, I'm curious about the
 >> "amount" of the expression that is evaluated lazily.
 >>
 >>
 >>   a.b().c().assumeWontThrow     vs.  a.b().c().assumeWontThrow
 >>   ^--+----^                                ^-^
 >>      |                                      |
 >>      +- lazy?                               +- lazy?

As John Colvin said, the first one is right. A test:

import std.stdio;

struct S
{}

S a(S s)
{
     writeln("entered a");
     return s;
}

S b(S s)
{
     writeln("entered b");
     return s;
}

void lazily(T)(lazy T expression)
{
     writeln("entered lazily");
     expression();
}

void main()
{
     S().a.b.lazily;
}

Outputs

entered lazily
entered a
entered b


 >> The video seems to say "don't use lazy functions with UFCS because you
 >> might think the lazy part gets evaluated first, when it does not".
 >> Seems reasonable, although I don't know it's any different than
 >> assumeWontThrow(f()).

You are right again. :) However, putting the lazy-taking function 
"outside" the whole expression makes it visible right away, making easy 
for me to realize that the execution order may be different from common 
chains.

Ali



More information about the Digitalmars-d-learn mailing list