An issue with lazy delegates
Andrej Mitrovic
andrej.mitrovich at gmail.com
Wed Jan 4 21:26:14 PST 2012
import std.stdio;
void test(T)(lazy T dg)
{
test2(dg);
}
void test2(T)(lazy T dg)
{
dg(); // nothing happens
dg()(); // have to use double-invocation instead
}
void main()
{
test({ writeln("test"); });
}
Do you think it would be possible for the compiler to avoid wrapping
delegates into another delegate? I'm having this problem with this
sort of template:
import std.conv;
import std.string;
auto onFailThrow(E, T)(lazy T dg)
{
try
{
static if (is(ReturnType!T == void))
dg();
else
return dg();
}
catch (Exception ex)
{
throw new E(ex.msg);
}
}
void main()
{
string x = "x";
string y = "y";
onFailThrow!Exception({ to!int(x); });
onFailThrow!Exception(to!int(y));
}
The first call doesn't do anything because the delegate is wrapped
inside of another delegate. I want this template to be versatile
enough to be used by both lazy expressions and delegate literals, but
I don't know how.
If I write the same template but with "lazy" stripped off I'll have
conflicting templates, but I don't know how I would write constraints
to separate the two. Any ideas?
More information about the Digitalmars-d-learn
mailing list