Template Prerequisite proposal

davidl davidl at 126.com
Mon Sep 17 20:28:31 PDT 2007


This proposal would break current template legacy code.

import std.stdio;
template k(T)
{
     void func(T t)
     in
     {
         static assert(is(T = int));		// prerequisite
     }
     body
     {
         writefln(t);		// we should constrain the use of type T, because  
it's checked as prerequisite with is(T = int)
				// so we can get it a little bit further.
         T v;
         t~=v;			// this template code should fail at compile time , and  
don't even need to instantiate this template
                                 // because we can't expect a type of is(T  
= int) can have any operator of ~ in this case
     }
}
void main()
{
     mixin k!(int);
}

This suggests another template writing style.


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.
    }
}

With the prerequisite checking in static invariant, we can apply  
restrictions in "static invariant" to the template code of using type T.



-- 
使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/



More information about the Digitalmars-d mailing list