Alias templates and anonymous delegates in pipe/map

Adrien Chauve adrien.chauve at gmail.com
Wed May 18 12:11:56 PDT 2011


Hi,

anyone on the alias difference?

Thanks,
Adrien

On Sat, May 14, 2011 at 20:12, Adrien Chauve <adrien.chauve at gmail.com>wrote:

> Hi,
>
> I've been playing with templates and I get some (really dumb) questions.
> By the way, I'm using dmd 2.053 64 bits on linux 64 bits.
>
>
> First, I didn't really get the difference between an alias template and a
> type template when they are both instanciated with a type.
> For example:
>
> //////// File test1.d
> // alias
> template
> template Foo(alias
> X)
> {
>
>     alias X
> Y;
> }
>
>
>
> // type
> template
>
> template
> Foo2(X)
> {
>
>     alias X
> Y;
> }
>
>
>
> struct Bar
> {}
>
>
> void
> main()
>
> {
>
>     alias Foo!(Bar).Y BarAlias; //
> ok
>     alias Foo2!(int).Y IntAlias; //
> ok
>     alias Foo!(int).Y OtherIntAlias; //
> error
> }
> ////// end of file test1.d
>
> I get the following error from dmd:
> test1.d(19): Error: template instance Foo!(int) does not match template
> declaration Foo(alias X)
> test1.d(19): Error: identifier 'Y' of 'Foo!(int).Y' is not defined
>
> Why do the two templates behave differently? Why does the alias template
> yield an error? What did I miss about alias templates?
>
>
>
>
> My second question is about using anonymous delegates in map and pipe. I
> tried the following code:
>
> //////// File test2.d
> import
> std.functional;
>
> import
> std.algorithm;
>
>
>
> unittest
>
> {
>
>     immutable auto x = [1, 2, 3,
> 4];
>
>
>     alias map!((i){return 2*i;})
> mult2;
>     assert(equal(mult2(x),
> [2,4,6,8]));
>
>
>     alias pipe!(mult2)
> pipe_mult2;
>     assert(equal(pipe_mult2(x), [2,4,6,8])); // ok
>
>
>
>     alias pipe!(mult2, mult2) pipe_mult4;      // error
>
>     assert(equal(pipe_mult4(x),
> [4,8,12,16]));
> }
>
>
>
> void main(){}
> /////////// end of file test2.d
>
> and when compiling it with dmd -unittest test2.d I get:
> test2.d(368): Error: function
> test2.__unittest3.map!(__dgliteral1).map!(immutable(int[])).map is a nested
> function and cannot be accessed from doIt
> test2.d(368): Error: function
> test2.__unittest3.map!(__dgliteral1).map!(Result).map is a nested function
> and cannot be accessed from doIt
>
> 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.
>
> I'm not sure to understand what's the error in compose (pipe uses compose).
> 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.
>
>
> Cheers,
> Adrien
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110518/00b8934b/attachment.html>


More information about the Digitalmars-d mailing list