Template Prerequisite proposal

DavidL davidl at 126.com
Mon Sep 17 20:58:58 PDT 2007


> 
> Last proposal actually already works in DMD
> 
> I want a little bit further:
> 
> class (T)
> {
>     static invariant
>     {
>        // we can do some concept check here
>     }
>     void func(T t)
>     {
>        T v;
>        v = v + t;       // we didn't have the opAdd check in static  
> invariant, in my proposal , this code should be rejected.
>     }
> }
>

robert gave me a better example for class:

template LionAssertions()   // prerequisite template
{
    static assert(Lion.foodChainPosition == FoodChain.max);
    static assert(Mufasa > Aslan, "this program contains evil lies");
}

class Lion : AwesomenessIncarnate
{
    mixin LionAssertions!()
}

For common case:

template k(T)
{
	static assert(T.max==int.max);
}

template a(T)
{
	void func(T t)
	in
	{
		mixin k!(T);
	}
	body
	{
	}
}

mixin a!(int);


so there's a unified way of mixin template for prerequisite checking.

I hope if there's any way of restrict template writer write code which only uses things which are checked in the prerequisite checking.



More information about the Digitalmars-d mailing list