Passing shared delegates
Martin Drasar
drasar at ics.muni.cz
Wed Jan 16 12:05:28 PST 2013
Okay, I have hit another thing when dealing with shared delegates.
Consider this code:
> alias void delegate (B b) shared Callback;
>
> class A
> {
> private B _b;
>
> this (B b)
> {
> _b = b;
> }
>
> void callback (B b) shared
> {
> b.execute(&callback);
> //_b.execute(&callback); <-- Uncomment this for error
> }
> }
>
> class B
> {
> void execute (Callback c)
> {
> c(this);
> }
> }
>
> void main()
> {
> auto b = new B();
> auto a = new A(b);
>
> b.execute(&a.callback);
> }
If I uncomment that line I get this error:
> Error: function main.B.execute (void delegate(B b) shared c) is not callable using argument types (void delegate(B b) shared) shared
This error probably appears because typeof b is B and typeof _b is
shared(B). My question is - why? Is it a bug or does the shared delegate
made it shared and it is ok? And what to do with it? I can cast away
shared of _b, but is it a correct and clean way?
Thanks for your input.
Martin
More information about the Digitalmars-d-learn
mailing list