Placement of shared does not allow to set a delegate
Tolga Cakiroglu via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 19 01:26:54 PDT 2014
Yes the understanding is correct. I simplified the code a little:
IMPORTANT: DMD 2.065 is used.
class Car{
void delegate() onEvent;
}
class Test{
this() shared
{
auto car = new shared Car();
assert( typeid( car.onEvent ).toString() == "shared(void
delegate())" );
assert( typeid( &car_onEvent1 ).toString() == "void delegate()"
);
assert( typeid( &car_onEvent2 ).toString() == "void delegate()"
);
assert( typeid( cast(shared)( &car_onEvent2 ) ).toString() ==
"shared(void delegate())" );
car.onEvent = &car_onEvent1;
car.onEvent = &car_onEvent2; // COMPILATION ERROR
car.onEvent = cast(shared)( &car_onEvent2 ); // COMPILATION
ERROR
}
void car_onEvent1(){}
void car_onEvent2() shared{}
}
void main(){
new shared Test();
}
I just want to know if I am doing a mistake, thinking in wrong
way, or there is a bug.
The first compilation error is that it cannot convert "void
delegate() shared" to "shared(void delegate())".
The second compilation error is that it cannot convert
"shared(void delegate())" to "shared(void delegate())".
(Since DMD 2.066 is not ready yet, I need to continue with 2.065.)
On Tuesday, 17 June 2014 at 04:35:10 UTC, Ali Çehreli wrote:
> I don't know but I theorize like this: :) There are three
> 'shared' qualifiers in the last error message.
>
> shared(void delegate(shared(SocketListener)) shared)
>
> 1) The 'shared' in the middle: That one makes the delegate take
> a shared(SocketListener) argument when it gets called.
>
> 2) The 'shared' on the right-hand side: (I am not sure about
> this one.) That one makes the context pointer of the delegate
> 'shared'. For example, the delegate can only be called on a
> shared class object.
>
> 3) The 'shared' on the left-hand side: That one makes the type
> of the delegate shared so that e.g. such a delegate can be
> passed between threads.
>
> That's what I understand.
>
> Ali
More information about the Digitalmars-d-learn
mailing list