Address of UFCS call implicity converts to Delegate

Jonathan Marler via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 23 09:32:06 PDT 2017


This feels like a natural extension to existing semantics. It 
doesn't require new syntax and serves as a solution to some 
issues when working with delegates.

Say some API wants a delegate like this: void delegate(string arg)

With this feature, you could take a function like this:

void myCoolFunction(MyClassObject obj, string arg)
{
     // ...
}

and the following would have the same delegate type:

&myClassObject.myCoolFunction // type is void delegate(string arg)



This of course wouldn't work for all functions.  The first 
parameter of the function would need to have the same calling 
convention as the "this" parameter for a "delegate".

void cantBecomeDelegate(SomeBigStructType s)
{
}
&myStruct.cantBecomeDelegate // Error

error: cannot convert UFCS call to delegate because the first 
parameter of function 'cantBecomeDelegate' is too large.  
Consider adding "ref" to the first parameter or making it a 
pointer.

To fix this you could do something like this:
void canBecomeDelegate(ref SomeBigStructType s)
{
}
&myStruct.canBecomeDelegate // OK!




More information about the Digitalmars-d mailing list