Passing shared delegates

Martin Drasar drasar at ics.muni.cz
Fri Jan 11 08:38:44 PST 2013


Hi,

when trying to compile this code:

> module main;
> 
> class A
> {
>   shared void foo() {}
> }
> 
> class B
> {
>   void bar(shared void delegate() f) {}
> }
> 
> void main()
> {
>   auto a = new A();
>   auto b = new B();
> 
>   b.bar(&a.foo);
> }

I get this error:

> main.d(18): Error: cannot implicitly convert expression (&a.foo) of type void delegate() shared to shared(void delegate())

I have searched the bugzilla and newsgroups and found various threads
related to shared and delegate passing and the most recent was about
casting it all away and then back in when needed. Is this the only
possible way to do it?

In my program I want to pass delegates to a running thread using
std.concurrency. I can do it by casting the delegate (or rather a
structure containing the delegate together with other stuff) to shared,
send it, cast it away after I receive it and then work with it. But that
does not feel right...

What exactly happens when I cast to and from shared? Does the object get
copied, moved, or any other magic? Is it costly operation?

Thanks for any hints,
Martin


More information about the Digitalmars-d-learn mailing list