www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alias templates and anonymous delegates in pipe/map

--e0cb4e43cef7e0964504a3405d85
Content-Type: text/plain; charset=ISO-8859-1

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

--e0cb4e43cef7e0964504a3405d85
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: base64

SGksPGJyPjxicj5JJiMzOTt2ZSBiZWVuIHBsYXlpbmcgd2l0aCB0ZW1wbGF0ZXMgYW5kIEkgZ2V0
IHNvbWUgKHJlYWxseSBkdW1iKSBxdWVzdGlvbnMuPGJyPkJ5IHRoZSB3YXksIEkmIzM5O20gdXNp
bmcgZG1kIDIuMDUzIDY0IGJpdHMgb24gbGludXggNjQgYml0cy48YnI+PGJyPjxicj5GaXJzdCwg
SSBkaWRuJiMzOTt0IHJlYWxseSBnZXQgdGhlIGRpZmZlcmVuY2UgYmV0d2VlbiBhbiBhbGlhcyB0
ZW1wbGF0ZSBhbmQgYSB0eXBlIHRlbXBsYXRlIHdoZW4gdGhleSBhcmUgYm90aCBpbnN0YW5jaWF0
ZWQgd2l0aCBhIHR5cGUuPGJyPgoKCkZvciBleGFtcGxlOjxicj48YnI+Ly8vLy8vLy8gRmlsZSB0
ZXN0MS5kPGJyPi8vIGFsaWFzIHRlbXBsYXRloKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCg
oKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoCA8YnI+dGVtcGxhdGUgRm9vKGFs
aWFzIFgpoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCg
oKCgoKCgoKCgoKCgIDxicj4KCgp7oKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCg
oKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKAgPGJyPqCgoCBh
bGlhcyBYIFk7oKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCg
oKCg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