Hi,<br><br>anyone on the alias difference?<br><br>Thanks,<br>Adrien<br><br><div class="gmail_quote">On Sat, May 14, 2011 at 20:12, Adrien Chauve <span dir="ltr"><<a href="mailto:adrien.chauve@gmail.com">adrien.chauve@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi,<br><br>I've been playing with templates and I get some (really dumb) questions.<br>By the way, I'm using dmd 2.053 64 bits on linux 64 bits.<br>
<br><br>First, I didn't really get the difference between an alias template and a type template when they are both instanciated with a type.<br>
For example:<br><br>//////// File test1.d<br>// alias template <br>template Foo(alias X) <br>
{ <br> alias X Y; <br>} <br>
<br>// type template <br>template Foo2(X) <br>
{ <br> alias X Y; <br>} <br>
<br>struct Bar {} <br> <br>
void main() <br>{ <br> alias Foo!(Bar).Y BarAlias; // ok <br>
alias Foo2!(int).Y IntAlias; // ok <br> alias Foo!(int).Y OtherIntAlias; // error <br>}<br>////// end of file test1.d<br><br>I get the following error from dmd:<br>
test1.d(19): Error: template instance Foo!(int) does not match template declaration Foo(alias X)<br>test1.d(19): Error: identifier 'Y' of 'Foo!(int).Y' is not defined<br><br>Why do the two templates behave differently? Why does the alias template yield an error? What did I miss about alias templates?<br>
<br><br><br><br>My second question is about using anonymous delegates in map and pipe. I tried the following code:<br><br>//////// File test2.d<br>import std.functional; <br>
import std.algorithm; <br> <br>unittest <br>
{ <br> immutable auto x = [1, 2, 3, 4]; <br> <br>
alias map!((i){return 2*i;}) mult2; <br> assert(equal(mult2(x), [2,4,6,8])); <br> <br>
alias pipe!(mult2) pipe_mult2; <br> assert(equal(pipe_mult2(x), [2,4,6,8])); // ok <br> <br>
alias pipe!(mult2, mult2) pipe_mult4; // error <br> assert(equal(pipe_mult4(x), [4,8,12,16])); <br>} <br>
<br>void main(){} <br>/////////// end of file test2.d<br><br>and when compiling it with dmd -unittest test2.d I get:<br>test2.d(368): Error: function test2.__unittest3.map!(__dgliteral1).map!(immutable(int[])).map is a nested function and cannot be accessed from doIt<br>
test2.d(368): Error: function test2.__unittest3.map!(__dgliteral1).map!(Result).map is a nested function and cannot be accessed from doIt<br><br>So a single pipe!mult2 is ok but when a I put a second there's an error. What's also interesting is that all cases work fine when I use a named function instead of an anonymous delegate.<br>
<br>I'm not sure to understand what's the error in compose (pipe uses compose).<br>And the error returned by dmd is a bit strange because in no way test2.d has more than 20 lines (in fact line 368 is the first line of map in std/algorithm.d). Besides, without a template instanciation stack trace, I think it's a bit difficult to dive into template errors.<br>
<br><br>Cheers,<br><font color="#888888">Adrien<br><br>
</font></blockquote></div><br>