Alias templates and anonymous delegates in pipe/map
Steven Schveighoffer
schveiguy at yahoo.com
Wed May 18 12:44:11 PDT 2011
On Wed, 18 May 2011 15:11:56 -0400, Adrien Chauve
<adrien.chauve at gmail.com> wrote:
> Hi,
>
> anyone on the alias difference?
I have a feeling that alias needs to be a symbol, and int is a keyword.
a template alias can be any symbol, including a variable, function,
delegate, etc.
Though it does seem unintuitive that a template alias parameter can be a
custom type, but not a keyword type. I would say that's a bug, but I'm
not sure.
> 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
>>
More information about the Digitalmars-d
mailing list