A couple of questions

Robert Fraser fraserofthenight at gmail.com
Wed May 13 05:22:13 PDT 2009


Simen Kjaeraas wrote:
> Sam Huwrote:
> 
>> Q4.In the delegate somFnExp:front(),popFront,empty() are all not 
>> defined??Anyway it is not an interface ,so why it is allowed?
> 
> Basically, is(typeof(X)) is D magic.
> 
> One could interpret it as 'is X a valid type', or perhaps more
> correctly as 'does X compile'. So if SomeFnExp does something it
> is not allowed to (e.g. call popFront on something that has no
> popFront), it will return false.
> 
> If I were to write this code without is(typeof()) around it:
> 
>  R r;
>  if (r.empty) {}
>  r.popFront;
>  auto h = r.front;
> 
> It might seem a strange piece of code, but there is nothing
> inherently wrong with it. empty, popFront and front are
> expected members of R, and will give a compilation error if R
> does not follow the interface (note: not as in a D interface, but
> as in exposing the correct functions to the outside world).
> 
> 
> -- 
>  Simen

IOW,

is(typeof({ XXXXXX }())

Evaluates to a boolean meaning "does a function containing XXXXXX 
compile". Its purpose here is to find out if r has "empty", "popFront" 
and "front". This is placed inside a template that has one member that 
is the name of the template, so it ends up that:

isInputRange!(R)

just becomes a boolean that's true if R has "empty", "popFront", and 
"front", and false if it doesn't.

... C++ template hacks are worse. Really. Usually.


More information about the Digitalmars-d-learn mailing list