How about a special null template parameter?

Engine Machine via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 19 11:25:06 PDT 2016


So we can create types relationships easier:

class Type(T) : Type!null
{
    int x;
    static if (T is Dog)
        int y;
}

Type == Type!null (!null implicit, only if defined in the above 
way. Essentially an alias is created for us automatically)

Type(T) : Type!nullinherits only if T is not null(or a type that 
inherits from itself,  which would then be valid).

Type!Dog inherits from Type/Type!null.

Type!T inherits from Type/Type!null as long as T != null.

Members in base(null) are not re-included in derived(e.g., int x; 
isn't include in Type!Dog because it's already there from base).

This simple syntactic sugar allows us to create multiple classes 
to create simple inheritance hierarchy.

It replaces the current method of having to define a 
non-templated class and a templated class.

e.g.,

class Type
{
    int x;
}

class Type(T) : Type
{
    static if (T is Dog)
       int y;
}



vs



class Type(T) : Type!null
{
    int x;
    static if (T is Dog)
        int y;
}









More information about the Digitalmars-d mailing list