On 05/01/2012 01:20 PM, Mehrdad wrote:
I guess the problem is with type deduction, but the trouble is that half
the reason why type deduction is useful is in the case of lambdas. They
become effectively useless if you have to type out their entire
signature, since you could then just make them a separate function (or
just use a mixin instead, to create a closure... which I've done before).
This isn't really an 'enhancement', since the error message is clearly a
bug. So I filed them both as bugs.
http://d.puremagic.com/issues/show_bug.cgi?id=8009
http://d.puremagic.com/issues/show_bug.cgi?id=8010
The other bug I was referring to was something along the lines of the
compiler telling me, "I can't really handle closures as template
parameters very well (something about 'local variable' or whatever), so
I'll just give you an obscure error every now and then".
It only happened when it was inside another function, and some
conditions were satisfied. (I don't remember the exact message, but when
I searched it, I saw it had been already reported before...)
Probably you mean this one:
struct S{
int x;
auto foo(alias a)(){return a(x);}
}
void main(){
auto s = S(2);
int y = 3;
writeln(s.foo!(x=>x+y)());
}
Error: template instance foo!(__lambda2) cannot use local
'__lambda2(__T1)' as parameter to non-global template foo(alias a)
This is an arbitrary restriction and should be fixed. The 'this' pointer
for 'foo' could be passed in a reserved slot at the beginning of the
context for 'main'.
Oh and thanks for the alternative, it's a good workaround to know. :)
The trouble is that while it solves the bug I posted, it's still not
solving my (actual) problem.
The actual problem was that I wanted to do something along the lines of:
private import std.range;
auto filter(R, F)(R r, F f) { /*return a filtered range*/ }
void main() { [1, 2, 3].filter(x => x < 3); }
private import std.range;
auto filter(R, F)(R r, F f) { /*return a filtered range*/ }
void main() { [1, 2, 3].filter((int x) => x < 3); }