How about a special null template parameter?

Engine Machine via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 19 15:07:26 PDT 2016


On Friday, 19 August 2016 at 21:07:42 UTC, Timon Gehr wrote:
> On 19.08.2016 20:25, Engine Machine wrote:
>> So we can create types relationships easier:
>>
>> class Type(T) : Type!null
>> {
>>    int x;
>>    static if (T is Dog)
>>        int y;
>
>
> alias Seq(T...)=T;
>
> template TypeParent(T...) if(T.length==1){
>     static if(is(typeof(T[0])==typeof(null))) alias TypeParent 
> = Seq!();
>     else alias TypeParent = Seq!(Type!null);
> }
>
> class Type(T...): TypeParent!T if(T.length==1){
>    int x;
>    static if (T is Dog)
>        int y;
> }

This is a bit verbose and not quite right (T is Dog should be 
something like T[0], or whatever).

It does essentially work. My only complaint is that it would be 
nice to be able to export an alias to Type!() = Type; in the 
namespace of the type being created. Doubt that D can do that!? 
If it can, then it should be an adequate solution.

That is

It would be nice to have something like

alias Type = Type!();
class Type(T...): TypeParent!T if(T.length==1){
     int x;
     static if (T is Dog)
         int y;
}

Again, I don't think D will allow this.


alias Seq(T...)=T;

template TypeParent(T...) {
     static if(is(typeof(T[0])==typeof(null))) alias TypeParent = 
Seq!();
     else alias TypeParent = Seq!(Type!null);
}

class Type(T...) : TypeParent!T
{
    int x;
    static if(T.length == 1)
    {
	static if (T[0] is Dog)
		int y;
    }
}

(note I removed the length checks, so we can create the base type 
and such)

I guess a such a method is nice to create compound types though:

Type!(Animal, Dog, Chihuahua)

and have a inheritance chain.

e.g.,


class Type(T...) : TypeParent!T
{
    int x;
    static if(T.length >= 1)
    {
	static if (T[0] is "Animal")
	{
		int z;
		static if (T[1] is "Dog")
			int y;
	}
    }
}


void main()
{
	Type!("Animal", "Dog") t;
}

I don't know if all this obeys inheritance though but it looks 
like it should.

Thanks!


More information about the Digitalmars-d mailing list