[Issue 7265] Function Literals where a keyword was omitted should be delegate even if inference.
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Jan 17 06:37:36 PST 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7265
--- Comment #9 from SHOO <zan77137 at nifty.com> 2012-01-17 06:37:31 PST ---
By the way, the reason why I set this Issue with regression is that I cause a
difficult problem with the following cases:
------
import std.stdio, std.functional;
// Library code
struct Control {
alias void delegate(Control o) Handler;
Handler[] _handlers;
void addHandler(H)(H hnd) {
_handlers ~= cast(Handler)hnd;
}
void addHandler2(H)(H hnd) if (is(H == delegate)) {
_handlers ~= cast(Handler)hnd;
}
// Workaround. It is settled if can handle either by toDelegate.
void addHandler3(H)(H hnd) {
_handlers ~= cast(Handler)toDelegate(hnd);
}
void call() {
foreach (h; _handlers) {
h(this);
}
}
}
// User code
void main() {
int i;
auto c = new Control;
// OK. This literal is inferred delegate.
c.addHandler( (Object o){ writeln(i); } );
// Error. This literal is inferred function pointer.
// c.addHandler( (Object o){ writeln("test"); } );
// Error. This literal is inferred function pointer, too.
// The constraint-if does not influence the type inference.
// c.addHandler2( (Object o){ writeln("test2"); } );
// Workaround.
c.addHandler3( (Object o){ writeln("test3"); } );
c.call();
}
------
When a library code has a definition such as Control, many breaking changes
befall user codes.
Actually, this way is used by DFL.
One of simple workaround is to use toDelegate with library codes.
I think that another solution is to let implicit conversion perform a role as
toDelegate.
I remember it has entered ML agenda several times. But I've forgotten the
conclusion.
Is there a person knowing the details?
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list