IsExpression question

Kirk McDonald kirklin.mcdonald at gmail.com
Sun Jul 9 15:28:14 PDT 2006


Even after writing most of a library founded on the thing, I still don't 
get something. What exactly is the expression

     is(T == function)

testing? I would expect it to test whether T is a function pointer type 
(like how "is(T == delegate)" tests whether T is a delegate type), but 
it doesn't.

[test.d]
import std.stdio;

void func(int i) {
     writefln(i);
}

void main() {
     alias void function(int) FN;
     FN fn = &func;
     static if (is(typeof(*FN) == function))
         writefln("1: It's a function.");
     static if (is(FN == function))
         writefln("2: It's a function.");
     static if (is(typeof(func) == function))
         writefln("3: It's a function.");
     static if (is(typeof(&func) == function))
         writefln("4: It's a function.");
     static if (is(void function(int) == function))
         writefln("5: It's a function.");
     static if (is(void delegate(int) == delegate))
         writefln("6: Well, delegates work.");
}
// EOF

When run, only 1, 3, and 6 are printed.

1 just baffles be. What is that? I'm dereferencing a type? Then I'm 
taking the type of THAT? Why does that even work?

2 is what I expect to work: I'm testing whether a function pointer type 
is a function pointer type, right? Wrong.

3 makes sense, I suppose. I'm testing whether a function is a function. 
Fine.

4 is also sort of strange, though in light of the fact that 2 doesn't 
work, I wouldn't expect this one to, either.

5 is just 2 without the alias.

6 shows that, yes, delegates work like I expect them to.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d mailing list