Passing shared delegates

Maxim Fomin maxim at maxim-fomin.ru
Sun Jan 13 23:56:41 PST 2013


On Monday, 14 January 2013 at 07:20:17 UTC, Martin Drašar wrote:
> Dne 11.1.2013 23:26, mist napsal(a):
>> Do not have time to test code right now but first guess it is 
>> related to
>> parsing differences for delegates and usual functions. 
>> Delegates can
>> have shared/const applied to both delegate type itself and 
>> context of
>> underlying function. Those are different beasts and no wonder 
>> type
>> system complains.
>>
>> You may need to try something like "void delegate() shared f" 
>> if you
>> want delegate type to match method one.
>
> Hi mist,
>
> that was the first thing I tried, but it resulted in a 
> completely different error:
>
>> class B
>> {
>>  void bar(void delegate() shared f) {}
>> }
>
>> Error: const/immutable/shared/inout attributes are only valid 
>> for non-static member functions
>
> Martin

Which compiler version do you use? It compiles on 2.061.

In case of applying attributes to functions, mostly it is 
irrelevant whether it stands first or last. So,

void foo() shared {}

and

shared void foo()  {}

in A class are equivalent. However in case of bar parameter you 
are qualifying not a function, but object, so shared before 
return type of delegate applies to object, like (shared (int i)). 
Shared after delegate applies to delegate type, not object itself.


More information about the Digitalmars-d-learn mailing list