Placement of shared does not allow to set a delegate

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 16 08:25:51 PDT 2014


On 06/16/2014 05:35 AM, Tolga Cakiroglu wrote:> In a class I defined an 
event attribute as follows:
 >
 > public void delegate( shared(SocketListener) sender ) eventWhenStarted;
 >
 > ---
 >
 > An instance of SocketListener is created.
 >
 > auto listener = new shared SocketListener();
 >
 > ---
 >
 > I defined a shared method that will be called when the event occurs.
 >
 > public void listener_whenStarted( shared(SocketListener) sender ) 
shared{}
 >
 > ---
 >
 > I set the event attribute of listener to defined method:
 >
 > listener.eventWhenStarted = &listener_whenStarted;
 >
 > ----
 >
 > Here is the error message:
 >
 > main.d(21): Error: cannot implicitly convert expression
 > (&this.listener_whenStarted) of type void
 > delegate(shared(SocketListener) sender) shared to shared(void
 > delegate(shared(SocketListener)))
 >
 >
 > If we look at types separately as follows:
 >
 > .......void delegate(shared(SocketListener) sender) shared
 > shared(void delegate(shared(SocketListener)))
 >
 >
 > Isn't the first type same as the second one?
 >
 >
 > (1.5 years later, I still hate the `shared` keyword. It is nothing but
 > confusion and extra coding for many programmers.)

Here is a minimal example that reproduces the issue and the hint that 
compiles the code:

class SocketListener
{
     public void delegate( shared(SocketListener) sender )
     /* shared */    // <-- UNCOMMENT TO COMPILE
     eventWhenStarted;
}

class C
{
     this()
     {
         auto listener = new shared SocketListener();
         listener.eventWhenStarted = &listener_whenStarted;
     }

     public void listener_whenStarted( shared(SocketListener) sender ) 
shared
     {}
}

void main()
{}

Error: cannot implicitly convert expression (&this.listener_whenStarted) 
of type void delegate(shared(SocketListener) sender) shared to 
shared(void delegate(shared(SocketListener)))

Ali



More information about the Digitalmars-d-learn mailing list