How about a special null template parameter?

Enamex via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 19 15:20:09 PDT 2016


On Friday, 19 August 2016 at 18:25:06 UTC, Engine Machine wrote:
> 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).

Something like this:

class Type(T: typeof(null)) { //< L1 (specialization)
     int x;
}

class Dog {}

class Type(T) : Type!(typeof(null)) { //< L2 (`typeof(null)`)
     static if(is(T: Dog)) //< L3 (`is(MyType: IntendedType)` or 
`is(MyType == ExactType)`)
         int y;
}

What you're looking for is "specialization", on line "L1". Also 
some corrections on lines "L2" and "L3"


More information about the Digitalmars-d mailing list