[Issue 4427] __traits should have isFunction, isVariable, etc

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jul 6 04:30:00 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4427



--- Comment #3 from Simen Kjaeraas <simen.kjaras at gmail.com> 2010-07-06 04:29:56 PDT ---
I agree. Many of these could easily be turned into templates in std.traits, and
perhaps they should.


template isClass( T ) {
    enum isClass = is( T == class );
}

unittest {
    interface I {}
    class A {}
    class B( T ) {}
    class C : B!int, I {}
    struct S {}

    static assert( isClass!A );
    static assert( isClass!( B!int ) );
    static assert( isClass!C );
    static assert( !isClass!I );
    static assert( !isClass!S );
    static assert( !isClass!int );
    static assert( !isClass!( int* ) );
}

template isInterface( T ) {
    enum isInterface = is( T == interface );
}

unittest {
    interface I {}
    class A {}
    class B( T ) {}
    class C : B!int, I {}
    struct S {}

    static assert( !isInterface!A );
    static assert( !isInterface!( B!int ) );
    static assert( !isInterface!C );
    static assert( isInterface!I );
    static assert( !isInterface!S );
    static assert( !isInterface!int );
    static assert( !isInterface!( int* ) );
}

template isStruct( T ) {
    enum isStruct = is( T == struct );
}

unittest {
    interface I {}
    class A {}
    class B( T ) {}
    class C : B!int, I {}
    struct S {}

    static assert( !isStruct!A );
    static assert( !isStruct!( B!int ) );
    static assert( !isStruct!C );
    static assert( !isStruct!I );
    static assert( isStruct!S );
    static assert( !isStruct!int );
    static assert( !isStruct!( int* ) );
}

template isType( T ) {
    enum isType = true;
}

template isType( alias T ) {
    enum isType = false;
}

unittest {
    struct S {
        alias int foo;
    }

    static assert( isType!int );
    static assert( isType!float );
    static assert( isType!string );
    //static assert( isType!S ); // Bugzilla 4431
    static assert( isType!( S.foo ) );
    static assert( !isType!4 );
    static assert( !isType!"Hello world!" );
}

I'm not entirely sure what you want isVariable to do. Does it check if
something can be assigned to (i.e. is variable), or are immutable and const
'variables' also variables?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list