Determining if a symbol is a function

Doctor J nobody at nowhere.com
Sun Apr 12 12:25:56 PDT 2009


I want to test whether a struct member is a real field or a property at compile time.  I thought this is what is(type == function) is supposed to do, but I can't find anything that will make is(type == function) true.  What am I doing wrong?

----------------------------------------------
import std.stdio;

int func0()
{
    return 0;
}

int func1(int x)
{
    return x;
}

int main()
{
    static if (is(func0))
        writefln("func0 is semantically correct.");
    static if (is(func0 == function))
        writefln("func0 is a function.");
    else
        writefln("func0 is NOT a function.");
    writefln("typeof(func0): %s\n", typeof(func0).stringof);
    
    static if (is (func0()))
        writefln("func0() is semantically correct.");
    static if (is(func0() == function))
        writefln("func0() is a function.");
    else
        writefln("func0() is NOT a function.");
    writefln("typeof(func0()): %s", typeof(func0()).stringof);
    
    // And for good measure, this one doesn't even compile; why is that?
    // writefln("typeof(func1): %s", typeof(func1).stringof);   
    return 0;
}
------------------------------------------

This outputs:

func0 is NOT a function.
typeof(func0): (int())()

func0() is NOT a function.
typeof(func0()): int



Oh, and I'm using Phobos 1.0 with gdc.  :)




More information about the Digitalmars-d-learn mailing list