www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - most non-function templates could be advantageously replaced by

reply Timothee Cour <thelastmammoth gmail.com> writes:
--089e0149529ec214e204dca820ac
Content-Type: text/plain; charset=ISO-8859-1

Phobos has a number of *non-function template* traits (std.traits,
std.typetuple etc), eg:

---- in std.traits:
template hasMember(T, string name) {
    static if (is(T == struct) || is(T == class) || is(T == union) || is(T
== interface))
        enum bool hasMember =
            staticIndexOf!(name, __traits(allMembers, T)) != -1 ||
            __traits(compiles, { mixin("alias Identity!(T."~name~") Sym;");
});
    else
        enum bool hasMember = false;
}
----

I see those *disadvantages* with non-function templates:

* syntax is less clear than regular function templates (there's no way to
tell the return type without looking at the code or using some convention
relating to the template name)

* they're not DRY, as the template name is typically repeated (maybe in
multiple places) inside the template body (hasMember appears 3 times here)

* behavior of templates is actually weird, for example:

----
 template A1(T){enum A1=0;}
template A2(T){enum B=0;}
template A3(T){enum A3=0;enum B=0;}
void main(){
    enum a1=A1!int;    //ok
    enum a2=A2!int.B;    //ok
    //    enum a3=A3!int.B;    //Error: no property 'B' for type 'int'
}
----

Maybe this was for historical reasons (ie CTFE might've come after those
design patterns were invented).

I would propose to use *regular template functions* instead, whenever
possible (essentially for the ones that return a value):

For our example, this would become:
----
 bool hasMember(T, string name)(){
    static if (is(T == struct) || is(T == class) || is(T == union) || is(T =
= interface))
        return staticIndexOf!(name, __traits(allMembers, T)) != -1 ||
                __traits(compiles, { mixin("alias Identity!(T."~name~") Sym;
"); });
    else return false;
}
// can be used in exactly the same way:
static assert(hasMember!(A,"field"));
----

*Advantages*:
* clear syntax: returns bool, and body is simpler to read as well
* DRY: hasMember only mentioned once
* no weird behavior as above
* doesn't break any code as usage is the same (backwards compatible)

In fact, I was able to convert with no problems a number of other such
templates:
 staticIndexOf
 isSame
 expectType
 genericIndexOf
etc...

For the latter (genericIndexOf), the template body contained a clause like
that:
 enum next  = genericIndexOf!(e, tail).index;
enum index = (next == -1) ? -1 : 1 + next;
during conversion, I just had to add the line:
return Tuple!(int, "index",int, "next")(index,next);
which could further be simplified using a helper function as its a common
idiom, into:
return TupleNamed!("index","next")(index,next);


*Limitations*:
One typical case where this isn't possible is when a template defines an
type alias, eg:
----
template Iota(int stop) {
    static if (stop <= 0)
        alias Iota=TypeTuple!();
    else
        alias Iota=TypeTuple!(Iota!(stop-1), stop-1);
}
----

--089e0149529ec214e204dca820ac
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Phobos has a number of <b>non-function template</b> traits (std.traits, std=
.typetuple etc), eg:<br><br>---- in std.traits:<br>template hasMember(T, st=
ring name)=A0{<br>=A0 =A0 static if (is(T =3D=3D struct) || is(T =3D=3D cla=
ss) || is(T =3D=3D union) || is(T =3D=3D interface))<br>
=A0 =A0 =A0 =A0 enum bool hasMember =3D<br>=A0 =A0 =A0 =A0 =A0 =A0 staticIn=
dexOf!(name, __traits(allMembers, T)) !=3D -1 ||<br>=A0 =A0 =A0 =A0 =A0 =A0=
 __traits(compiles, { mixin(&quot;alias Identity!(T.&quot;~name~&quot;) Sym=
;&quot;); });<br>=A0 =A0 else<br>=A0 =A0 =A0 =A0 enum bool hasMember =3D fa=
lse;<br>
}<br>----<br><br>I see those <b>disadvantages</b> with non-function templat=
es:<div><br>* syntax is less clear than regular function templates (there&#=
39;s no way to tell the return type without looking at the code or using so=
me convention relating to the template name)<br>
<br></div><div>* they&#39;re not DRY, as the template name is typically rep=
eated (maybe in multiple places) inside the template body (hasMember appear=
s 3 times here)</div><div><br></div><div>* behavior of templates is actuall=
y weird, for example:</div>
<div><br></div><div>----</div><div>



<font face=3D"Menlo">
<span style=3D"color:#009695">template</span><span style=3D"color:#444444">=
=A0</span><span style=3D"color:#444444">A1</span><span style=3D"color:#4444=
44">(</span><span style=3D"color:#444444">T</span><span style=3D"color:#444=
444">)</span><span style=3D"color:#444444">{</span><span style=3D"color:#00=
9695">enum</span><span style=3D"color:#444444">=A0</span><span style=3D"col=
or:#444444">A1</span><span style=3D"color:#444444">=3D</span><span style=3D=
"color:#f57d00">0</span><span style=3D"color:#444444">;</span><span style=
=3D"color:#444444">}</span><br>

<span style=3D"color:#009695">template</span><span style=3D"color:#444444">=
=A0</span><span style=3D"color:#444444">A2</span><span style=3D"color:#4444=
44">(</span><span style=3D"color:#444444">T</span><span style=3D"color:#444=
444">)</span><span style=3D"color:#444444">{</span><span style=3D"color:#00=
9695">enum</span><span style=3D"color:#444444">=A0</span><span style=3D"col=
or:#444444">B</span><span style=3D"color:#444444">=3D</span><span style=3D"=
color:#f57d00">0</span><span style=3D"color:#444444">;</span><span style=3D=
"color:#444444">}</span><br>

<span style=3D"color:#009695">template</span><span style=3D"color:#444444">=
=A0</span><span style=3D"color:#444444">A3</span><span style=3D"color:#4444=
44">(</span><span style=3D"color:#444444">T</span><span style=3D"color:#444=
444">)</span><span style=3D"color:#444444">{</span><span style=3D"color:#00=
9695">enum</span><span style=3D"color:#444444">=A0</span><span style=3D"col=
or:#444444">A3</span><span style=3D"color:#444444">=3D</span><span style=3D=
"color:#f57d00">0</span><span style=3D"color:#444444">;</span><span style=
=3D"color:#009695">enum</span><span style=3D"color:#444444">=A0</span><span=
 style=3D"color:#444444">B</span><span style=3D"color:#444444">=3D</span><s=
pan style=3D"color:#f57d00">0</span><span style=3D"color:#444444">;</span><=
span style=3D"color:#444444">}</span><br>

<span style=3D"color:#009695">void</span><span style=3D"color:#444444">=A0<=
/span><span style=3D"color:#444444">main</span><span style=3D"color:#444444=
">(</span><span style=3D"color:#444444">)</span><span style=3D"color:#44444=
4">{</span><br>

<span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096=
95">enum</span><span style=3D"color:#444444">=A0</span><span style=3D"color=
:#444444">a1</span><span style=3D"color:#444444">=3D</span><span style=3D"c=
olor:#444444">A1</span><span style=3D"color:#444444">!</span><span style=3D=
"color:#009695">int</span><span style=3D"color:#444444">;</span><span style=
=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"font-style:italic;colo=
r:#999988">//</span><span style=3D"font-style:italic;color:#999988">ok</spa=
n><br>

<span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096=
95">enum</span><span style=3D"color:#444444">=A0</span><span style=3D"color=
:#444444">a2</span><span style=3D"color:#444444">=3D</span><span style=3D"c=
olor:#444444">A2</span><span style=3D"color:#444444">!</span><span style=3D=
"color:#009695">int</span><span style=3D"color:#444444">.</span><span style=
=3D"color:#444444">B</span><span style=3D"color:#444444">;</span><span styl=
e=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"font-style:italic;col=
or:#999988">//</span><span style=3D"font-style:italic;color:#999988">ok</sp=
an><br>

<span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"font-style:=
italic;color:#999988">//</span><span style=3D"font-style:italic;color:#9999=
88">=A0=A0=A0=A0</span><span style=3D"font-style:italic;color:#999988">enum=
</span><span style=3D"font-style:italic;color:#999988">=A0</span><span styl=
e=3D"font-style:italic;color:#999988">a3</span><span style=3D"font-style:it=
alic;color:#999988">=3D</span><span style=3D"font-style:italic;color:#99998=
8">A3</span><span style=3D"font-style:italic;color:#999988">!</span><span s=
tyle=3D"font-style:italic;color:#999988">int</span><span style=3D"font-styl=
e:italic;color:#999988">.</span><span style=3D"font-style:italic;color:#999=
988">B</span><span style=3D"font-style:italic;color:#999988">;</span><span =
style=3D"font-style:italic;color:#999988">=A0=A0=A0=A0</span><span style=3D=
"font-style:italic;color:#999988">/</span><span style=3D"font-style:italic;=
color:#999988">/</span><span style=3D"font-style:italic;color:#999988">Erro=
r</span><span style=3D"font-style:italic;color:#999988">:</span><span style=
=3D"font-style:italic;color:#999988">=A0</span><span style=3D"font-style:it=
alic;color:#999988">no</span><span style=3D"font-style:italic;color:#999988=
">=A0</span><span style=3D"font-style:italic;color:#999988">property</span>=
<span style=3D"font-style:italic;color:#999988">=A0</span><span style=3D"fo=
nt-style:italic;color:#999988">&#39;</span><span style=3D"font-style:italic=
;color:#999988">B</span><span style=3D"font-style:italic;color:#999988">&#3=
9;</span><span style=3D"font-style:italic;color:#999988">=A0</span><span st=
yle=3D"font-style:italic;color:#999988">for</span><span style=3D"font-style=
:italic;color:#999988">=A0</span><span style=3D"font-style:italic;color:#99=
9988">type</span><span style=3D"font-style:italic;color:#999988">=A0</span>=
<span style=3D"font-style:italic;color:#999988">&#39;</span><span style=3D"=
font-style:italic;color:#999988">int</span><span style=3D"font-style:italic=
;color:#999988">&#39;</span><br>

<span style=3D"color:#444444">}</span></font>

</div><div><div>----</div></div><div><br></div><div>Maybe this was for hist=
orical reasons (ie CTFE might&#39;ve come after those design patterns were =
invented).</div><div><br></div><div>I would propose to use <b>regular templ=
ate functions</b> instead, whenever possible (essentially for the ones that=
 return a value):</div>
<div><br></div><div>For our example, this would become:</div><div>----</div=
<div>

<font face=3D"Menlo"> <span style=3D"color:#009695">bool</span><span style=3D"color:#444444">=A0<= /span><span style=3D"color:#444444">hasMember</span><span style=3D"color:#4= 44444">(</span><span style=3D"color:#444444">T</span><span style=3D"color:#= 444444">,</span><span style=3D"color:#444444">=A0</span><span style=3D"colo= r:#009695">string</span><span style=3D"color:#444444">=A0</span><span style= =3D"color:#444444">name</span><span style=3D"color:#444444">)</span><span s= tyle=3D"color:#444444">(</span><span style=3D"color:#444444">)</span><span = style=3D"color:#444444">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">static</span><span style=3D"color:#444444">=A0</span><span style=3D"col= or:#009695">if</span><span style=3D"color:#444444">=A0</span><span style=3D= "color:#444444">(</span><span style=3D"color:#009695">is</span><span style= =3D"color:#444444">(</span><span style=3D"color:#444444">T</span><span styl= e=3D"color:#444444">=A0</span><span style=3D"color:#444444">=3D</span><span= style=3D"color:#444444">=3D</span><span style=3D"color:#444444">=A0</span>= <span style=3D"color:#009695">struct</span><span style=3D"color:#444444">)<= /span><span style=3D"color:#444444">=A0</span><span style=3D"color:#444444"=
|</span><span style=3D"color:#444444">|</span><span style=3D"color:#444444=

4444">(</span><span style=3D"color:#444444">T</span><span style=3D"color:#4= 44444">=A0</span><span style=3D"color:#444444">=3D</span><span style=3D"col= or:#444444">=3D</span><span style=3D"color:#444444">=A0</span><span style= =3D"color:#009695">class</span><span style=3D"color:#444444">)</span><span = style=3D"color:#444444">=A0</span><span style=3D"color:#444444">|</span><sp= an style=3D"color:#444444">|</span><span style=3D"color:#444444">=A0</span>= <span style=3D"color:#009695">is</span><span style=3D"color:#444444">(</spa= n><span style=3D"color:#444444">T</span><span style=3D"color:#444444">=A0</= span><span style=3D"color:#444444">=3D</span><span style=3D"color:#444444">= =3D</span><span style=3D"color:#444444">=A0</span><span style=3D"color:#009= 695">union</span><span style=3D"color:#444444">)</span><span style=3D"color= :#444444">=A0</span><span style=3D"color:#444444">|</span><span style=3D"co= lor:#444444">|</span><span style=3D"color:#444444">=A0</span><span style=3D= "color:#009695">is</span><span style=3D"color:#444444">(</span><span style= =3D"color:#444444">T</span><span style=3D"color:#444444">=A0</span><span st= yle=3D"color:#444444">=3D</span><span style=3D"color:#444444">=3D</span><sp= an style=3D"color:#444444">=A0</span><span style=3D"color:#009695">interfac= e</span><span style=3D"color:#444444">)</span><span style=3D"color:#444444"=
)</span><br>

">=A0=A0=A0=A0</span><span style=3D"color:#009695">return</span><span style= =3D"color:#444444">=A0</span><span style=3D"color:#444444">staticIndexOf</s= pan><span style=3D"color:#444444">!</span><span style=3D"color:#444444">(</= span><span style=3D"color:#444444">name</span><span style=3D"color:#444444"=
,</span><span style=3D"color:#444444">=A0</span><span style=3D"color:#0096=

or:#444444">allMembers</span><span style=3D"color:#444444">,</span><span st= yle=3D"color:#444444">=A0</span><span style=3D"color:#444444">T</span><span= style=3D"color:#444444">)</span><span style=3D"color:#444444">)</span><spa= n style=3D"color:#444444">=A0</span><span style=3D"color:#444444">!</span><= span style=3D"color:#444444">=3D</span><span style=3D"color:#444444">=A0</s= pan><span style=3D"color:#444444">-</span><span style=3D"color:#f57d00">1</= span><span style=3D"color:#444444">=A0</span><span style=3D"color:#444444">= |</span><span style=3D"color:#444444">|</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#444444">=A0=A0=A0=A0</span><sp= an style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#009695"=
__traits</span><span style=3D"color:#444444">(</span><span style=3D"color:=

=3D"color:#444444">=A0</span><span style=3D"color:#444444">{</span><span st= yle=3D"color:#444444">=A0</span><span style=3D"color:#009695">mixin</span><= span style=3D"color:#444444">(</span><span style=3D"color:#f57d00">&quot;</= span><span style=3D"color:#f57d00">alias=A0Identity!(T.</span><span style= =3D"color:#f57d00">&quot;</span><span style=3D"color:#444444">~</span><span= style=3D"color:#444444">name</span><span style=3D"color:#444444">~</span><= span style=3D"color:#f57d00">&quot;</span><span style=3D"color:#f57d00">)= =A0Sym;</span><span style=3D"color:#f57d00">&quot;</span><span style=3D"col= or:#444444">)</span><span style=3D"color:#444444">;</span><span style=3D"co= lor:#444444">=A0</span><span style=3D"color:#444444">}</span><span style=3D= "color:#444444">)</span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0 =A0=A0</span><span style=3D"color:#009695= ">else=A0</span></font><span style=3D"font-family:Menlo;color:rgb(0,150,149= )">return</span><span style=3D"font-family:Menlo;color:rgb(68,68,68)">=A0</= span><span style=3D"font-family:Menlo;color:rgb(0,150,149)">false</span><sp= an style=3D"font-family:Menlo;color:rgb(68,68,68)">;</span><font face=3D"Me= nlo"><br> <span style=3D"color:#444444">}</span><br> </font></div><div><font face=3D"Menlo"><span style=3D"color:#444444">// can= be used in exactly the same way:</span></font></div><div><font face=3D"Men= lo"><span style=3D"color:#444444">static assert(hasMember!(A,&quot;field&qu= ot;));</span></font></div> <div><div>----</div></div><div><br></div><div><b>Advantages</b>:</div><div>= <div>* clear syntax: returns bool, and body is simpler to read as well</div=
</div><div><div>* DRY:=A0<span style=3D"color:rgb(68,68,68);font-family:Me=

</div><div><div>* no weird behavior as above</div></div><div>*=A0<span styl= e=3D"color:rgb(68,68,68);font-family:Menlo">doesn&#39;t break any code as u= sage is the same (backwards compatible)</span></div><div><font color=3D"#44= 4444" face=3D"Menlo"><br> </font></div><div>In fact, I was able to convert with no problems a number = of other such templates:</div><div> <font face=3D"Menlo"> <span style=3D"color:#444444"></span><span style=3D"color:#444444">staticIn= dexOf</span></font> </div><div> <font face=3D"Menlo"> <span style=3D"color:#444444"></span><span style=3D"color:#444444">isSame</= span></font> </div><div> <font face=3D"Menlo"> <span style=3D"color:#444444"></span><span style=3D"color:#444444">expectTy= pe</span></font> </div><div> <font face=3D"Menlo"> <span style=3D"color:#444444"></span><span style=3D"color:#444444">genericI= ndexOf</span></font> </div><div><font face=3D"Menlo"><span style=3D"color:#444444">etc...</span>= </font></div><div><br></div><div>For the latter (<span style=3D"color:rgb(6= 8,68,68);font-family:Menlo">genericIndexOf)</span>, the template body conta= ined a clause like that:</div> <div> <font face=3D"Menlo"> <span style=3D"color:#444444"></span><span style=3D"color:#009695">enum</sp= an><span style=3D"color:#444444">=A0</span><span style=3D"color:#444444">ne= xt</span><span style=3D"color:#444444">=A0</span><span style=3D"color:#4444= 44">=A0</span><span style=3D"color:#444444">=3D</span><span style=3D"color:= #444444">=A0</span><span style=3D"color:#444444">genericIndexOf</span><span= style=3D"color:#444444">!</span><span style=3D"color:#444444">(</span><spa= n style=3D"color:#444444">e</span><span style=3D"color:#444444">,</span><sp= an style=3D"color:#444444">=A0</span><span style=3D"color:#444444">tail</sp= an><span style=3D"color:#444444">)</span><span style=3D"color:#444444">.</s= pan><span style=3D"color:#444444">index</span><span style=3D"color:#444444"=
;</span><br>

/span><span style=3D"color:#444444">index</span><span style=3D"color:#44444= 4">=A0</span><span style=3D"color:#444444">=3D</span><span style=3D"color:#= 444444">=A0</span><span style=3D"color:#444444">(</span><span style=3D"colo= r:#444444">next</span><span style=3D"color:#444444">=A0</span><span style= =3D"color:#444444">=3D</span><span style=3D"color:#444444">=3D</span><span = style=3D"color:#444444">=A0</span><span style=3D"color:#444444">-</span><sp= an style=3D"color:#f57d00">1</span><span style=3D"color:#444444">)</span><s= pan style=3D"color:#444444">=A0</span><span style=3D"color:#444444">?</span=
<span style=3D"color:#444444">=A0</span><span style=3D"color:#444444">-</s=

</span><span style=3D"color:#444444">:</span><span style=3D"color:#444444">= =A0</span><span style=3D"color:#f57d00">1</span><span style=3D"color:#44444= 4">=A0</span><span style=3D"color:#444444">+</span><span style=3D"color:#44= 4444">=A0</span><span style=3D"color:#444444">next</span><span style=3D"col= or:#444444">;</span><br> </font></div><div>during conversion, I just had to add the line:</div><div>= <div><font face=3D"Menlo"><span style=3D"color:rgb(0,150,149)">return</span=
<span style=3D"color:rgb(68,68,68)">=A0</span><span style=3D"color:rgb(68,=

=3D"color:rgb(68,68,68)">(</span><span style=3D"color:rgb(0,150,149)">int</= span><span style=3D"color:rgb(68,68,68)">,</span><span style=3D"color:rgb(6= 8,68,68)">=A0</span><span style=3D"color:rgb(245,125,0)">&quot;</span><span= style=3D"color:rgb(245,125,0)">index</span><span style=3D"color:rgb(245,12= 5,0)">&quot;</span><span style=3D"color:rgb(68,68,68)">,</span><span style= =3D"color:rgb(0,150,149)">int</span><span style=3D"color:rgb(68,68,68)">,</= span><span style=3D"color:rgb(68,68,68)">=A0</span><span style=3D"color:rgb= (245,125,0)">&quot;</span><span style=3D"color:rgb(245,125,0)">next</span><= span style=3D"color:rgb(245,125,0)">&quot;</span><span style=3D"color:rgb(6= 8,68,68)">)</span><span style=3D"color:rgb(68,68,68)">(</span><span style= =3D"color:rgb(68,68,68)">index</span><span style=3D"color:rgb(68,68,68)">,<= /span><span style=3D"color:rgb(68,68,68)">next</span><span style=3D"color:r= gb(68,68,68)">)</span><span style=3D"color:rgb(68,68,68)">;</span></font></= div> </div><div>which could further be simplified using a helper function as its= a common idiom, into:</div><div><div><div><font face=3D"Menlo"><span style= =3D"color:rgb(0,150,149)">return</span><span style=3D"color:rgb(68,68,68)">= =A0</span><span style=3D"color:rgb(68,68,68)">TupleNamed</span><span style= =3D"color:rgb(68,68,68)">!</span><span style=3D"color:rgb(68,68,68)">(</spa= n><span style=3D"color:rgb(245,125,0)">&quot;</span><span style=3D"color:rg= b(245,125,0)">index</span><span style=3D"color:rgb(245,125,0)">&quot;</span=
<span style=3D"color:rgb(68,68,68)">,</span><span style=3D"color:rgb(245,1=

tyle=3D"color:rgb(245,125,0)">&quot;</span><span style=3D"color:rgb(68,68,6= 8)">)</span><span style=3D"color:rgb(68,68,68)">(</span><span style=3D"colo= r:rgb(68,68,68)">index</span><span style=3D"color:rgb(68,68,68)">,</span><s= pan style=3D"color:rgb(68,68,68)">next</span><span style=3D"color:rgb(68,68= ,68)">)</span><span style=3D"color:rgb(68,68,68)">;</span></font></div> </div></div><div><font face=3D"Menlo"><span style=3D"color:rgb(68,68,68)"><= br></span></font></div><div><br></div><div><b>Limitations</b>:=A0</div><div=
One typical case where this isn&#39;t possible is when a template defines =

<div>----</div><div><font face=3D"Menlo"><span style=3D"color:#009695">temp= late</span><span style=3D"color:#444444">=A0</span><span style=3D"color:#44= 4444">Iota</span><span style=3D"color:#444444">(</span><span style=3D"color= :#009695">int</span><span style=3D"color:#444444">=A0</span><span style=3D"= color:#444444">stop</span><span style=3D"color:#444444">)</span><span style= =3D"color:#444444">=A0</span><span style=3D"color:#444444">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">static</span><span style=3D"color:#444444">=A0</span><span style=3D"col= or:#009695">if</span><span style=3D"color:#444444">=A0</span><span style=3D= "color:#444444">(</span><span style=3D"color:#444444">stop</span><span styl= e=3D"color:#444444">=A0</span><span style=3D"color:#444444">&lt;</span><spa= n style=3D"color:#444444">=3D</span><span style=3D"color:#444444">=A0</span=
<span style=3D"color:#f57d00">0</span><span style=3D"color:#444444">)</spa=

<span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#009695">alias</span><span styl= e=3D"color:#444444">=A0</span></font><span style=3D"color:rgb(68,68,68);fon= t-family:Menlo">Iota=3D</span><span style=3D"font-family:Menlo;color:rgb(68= ,68,68)">TypeTuple</span><span style=3D"font-family:Menlo;color:rgb(68,68,6= 8)">!</span><span style=3D"font-family:Menlo;color:rgb(68,68,68)">(</span><= span style=3D"font-family:Menlo;color:rgb(68,68,68)">)</span><span style=3D= "font-family:Menlo;color:rgb(68,68,68)">;</span></div> <div><font face=3D"Menlo"> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">else</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#009695">alias</span><span styl= e=3D"color:#444444">=A0</span></font><span style=3D"color:rgb(68,68,68);fon= t-family:Menlo">Iota=3D</span><span style=3D"font-family:Menlo;color:rgb(68= ,68,68)">TypeTuple</span><span style=3D"font-family:Menlo;color:rgb(68,68,6= 8)">!</span><span style=3D"font-family:Menlo;color:rgb(68,68,68)">(</span><= span style=3D"font-family:Menlo;color:rgb(68,68,68)">Iota</span><span style= =3D"font-family:Menlo;color:rgb(68,68,68)">!</span><span style=3D"font-fami= ly:Menlo;color:rgb(68,68,68)">(</span><span style=3D"font-family:Menlo;colo= r:rgb(68,68,68)">stop</span><span style=3D"font-family:Menlo;color:rgb(68,6= 8,68)">-</span><span style=3D"font-family:Menlo;color:rgb(245,125,0)">1</sp= an><span style=3D"font-family:Menlo;color:rgb(68,68,68)">)</span><span styl= e=3D"font-family:Menlo;color:rgb(68,68,68)">,</span><span style=3D"font-fam= ily:Menlo;color:rgb(68,68,68)">=A0</span><span style=3D"font-family:Menlo;c= olor:rgb(68,68,68)">stop</span><span style=3D"font-family:Menlo;color:rgb(6= 8,68,68)">-</span><span style=3D"font-family:Menlo;color:rgb(245,125,0)">1)= </span><span style=3D"font-family:Menlo;color:rgb(68,68,68)">;</span></div> <div><font face=3D"Menlo"> <span style=3D"color:#444444">}</span></font>=A0=A0<br><div>----</div><div>= <br></div><br><br></div> --089e0149529ec214e204dca820ac--
May 13 2013
next sibling parent "Dicebot" <m.strashun gmail.com> writes:
On Tuesday, 14 May 2013 at 06:56:23 UTC, Timothee Cour wrote:
 ...

My conclusion as well. Currently I follow simple rule when doing meta-programming in D: * If stuff returns type, make it a template and write in functional recursive flavor * If stuff returns data, make it a function (template function if needed) and just let CTFE do the magic. One major advantage is that it helps to reduce template instance count and thus compiler memory consumption.
May 14 2013
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 14 May 2013 02:56:09 -0400, Timothee Cour  
<thelastmammoth gmail.com> wrote:

 *Limitations*:
 One typical case where this isn't possible is when a template defines an
 type alias, eg:
 ----
 template Iota(int stop) {
     static if (stop <= 0)
         alias Iota=TypeTuple!();
     else
         alias Iota=TypeTuple!(Iota!(stop-1), stop-1);
 }
 ----

Another is that CTFE can hit a brick wall and fail to compile. It's an implementation bug, but something to consider before changing everything to CTFE. AFAIK, straight templates don't have this limitation. There was quite a bit of talk at the conference about how CTFE is awesome, but too limited based on the current D front end. Otherwise, I like the idea. If nothing else, it gives more flexibility. -Steve
May 14 2013
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Tuesday, May 14, 2013 10:27:38 Dicebot wrote:
 On Tuesday, 14 May 2013 at 06:56:23 UTC, Timothee Cour wrote:
 ...

My conclusion as well. Currently I follow simple rule when doing meta-programming in D: * If stuff returns type, make it a template and write in functional recursive flavor * If stuff returns data, make it a function (template function if needed) and just let CTFE do the magic. One major advantage is that it helps to reduce template instance count and thus compiler memory consumption.

I believe that the typical approach at this point is that if it's something that will only ever be done at compile time and/or is querying about a type, an eponymous template is used, whereas if it's something that might be useful at runtime, then a normal function is used which is then CTFEable. I've never really thought about making the type stuff into functions, but I don't really agree that it's a problem that they aren't, particularly since it makes no sense to use them at runtime, and at least with eponymous templates, even if you try and use them at runtime, the result is still calculated at compile time, unlike with CTFE. I'm not sure that it actually matters much, but that _would_ result in a slight performance penalty for any code that was using traits in runtime code for some reason (though again, I don't know why you would), and as Steven pointed out, CTFE carries a definite performance penalty at compile time at the moment (though hopefully that's temporary). My gut reaction is definitely to keep doing traits as we have been, and it does seem off to me to make it so that something that's purely meant for compile time can be used at runtime, but it may make sense to change them. - Jonathan M Davis
May 14 2013