__traits(getDelegate, ...)
Steven Schveighoffer
schveiguy at yahoo.com
Sun Feb 3 21:14:58 PST 2013
I wanted to put forth again a proposal I had for getting a delegate to an
overloaded function. Only because I think it's relevant to the latest
property debate.
When getting the address of a delegate, it's as simple as:
struct S
{
void foo(int x) {}
}
S s;
auto dg = &s.foo; // delegate obtained
However, if foo is overloaded, this becomes more complex:
struct S
{
void foo(int x) {}
void foo(string s) {}
}
S s;
auto dg = &s.foo; // which one? Compiler chooses!
Turns out, it's somewhat possible to specify, but it can be
painful/uintuitive:
auto dg = cast(void delegate(string))&s.foo; // oops, what if I use the
wrong delegate signature, or S.foo is changed later!
How about getting a delegate using __traits?
auto dg = __traits(getDelegate, s.foo, string);
Maybe not quite as pretty as &s.foo, but at least allows you to form a
good expression without a cast.
Now, why is this relevant now? It could be useful for properties as
well. It could allow obtaining a delegate to a property (arguably a very
rare occurrence), where the current property proposal uses a funky
parentheses technique for it (which seems unintuitive for a C expression
to be changed by adding parentheses).
Full proposal:
__traits(getDelegate, symbol, arg1, arg2, ..., argN)
The args are optional, only needed if overloads exist. If overloads
exist, args MUST be specified (to protect against future changes). In the
special case where one of the overloads has no arguments, void must be
specified for that overload.
Rules/syntax subject to debate.
Destroy!
-Steve
More information about the Digitalmars-d
mailing list