2 Templates Issues (possibly bugs)
John Kiro
johnkirollos at yahoo.com
Sun Oct 7 09:38:09 PDT 2007
Hello there
I've been experimenting with templates & template mixins, and I have some questions & notes, here are they one by one:
1- I define a template like this:
template Template1(T, alias Var, alias Func)
{
T abc = Func(); //OK.
T xyz = Var; //#1 Error: non-constant expression i1
//Question 1: Why do I have this error?
}
The above error is issued in case I instantiate the template like this for ex:
Template1!(int, i1, Func1).abc = 3;
Or
Template1!(int, i1, Func1).xyz = 3;
No error is issued in case I instantiate it using template mixin:
mixin Template1!(int, i1, Func1);
The normal instantiation works only in case the template is defined like this:
template Template2(T, alias TFunc1, alias TFunc2)
{
T var1 = TFunc1();
T var2 = TFunc2();
}
I.e. both are aliases to functions, not variables. So is this a bug?
Note: I used 2 aliases in the above templates, but the behavior is general (i.e. even if template has only 1 alias)
2- Let's define 2 functions to be used with the previous template (template2):
int Func1()
{
return 100;
}
int Func2()
{
return 200;
}
Now the next instantiation fails:
Template2!(int,Func1,Func2).var1; //Error: var has no effect in expression (var1)
The next one succeeds:
Template2!(int,Func1,Func2).var1=10;
Next one succeeds as well (even if the instance is not defined before it):
writefln("var1 = ",Template2!(int,Func1,Func2).var1); //value is 100 (as returned by Func1() )
Template2!(int,Func1,Func2).var1+=30; //value is now 130
The question here is why the 1st one fails?
Also I see a confusion in the expression "Template2!(int,Func1,Func2).var1", it's sometimes equivalent to variable declaration (as in the previous writefln), and sometimes it's equivalent to a symbol (as in the "+=30" statement).. I hope this note would be added to the documentation.
Regards
John
More information about the Digitalmars-d
mailing list