Discussion Thread: DIP 1032--Function pointers and Delegate Parameters...--Community Review Round 1

Steven Schveighoffer schveiguy at gmail.com
Fri Apr 10 12:22:04 UTC 2020


On 4/10/20 5:40 AM, Walter Bright wrote:
> On 4/7/2020 2:36 PM, Steven Schveighoffer wrote:
>> For example, if you have:
>>
>> void foo(void delegate() dg) { dg(); }
>>
>> Let's say I'm in a @nogc function, and I want foo to call my local 
>> lambda @nogc delegate. Sure, I can pass in a delegate that is @nogc to 
>> this function, because of the implicit cast allowed. but I can't 
>> actually call the function from that context! It becomes useless.
> 
> This is still asking for (in effect) making foo() pure if the delegate 
> is pure. This is just not going to work if foo() does much more than 
> just call the delegate. Not many non-trivial functions can be pure, but 
> delegates often are pure, because delegates are often trivial.

No, you don't "make" foo pure, it is specifically marked pure.

In other words, you have:

void foo(void delegate() @called) pure @nogc @safe { dg(); }

So the @called attribute says "foo calls dg", which means that for a 
specific call of foo, you can strip off attributes that it doesn't have.

So if you pass in a @nogc pure delegate, that call of foo is considered 
@nogc pure. If you pass in a @safe pure delegate, that call of foo 
becomes @safe pure. If you pass in a nothrow delegate, it does NOT 
become nothrow, because the function did not declare that everything 
other than the delegate call is nothrow.

> 
> In order for your proposal to work, foo()'s implementation has to always 
> be the most restrictive, i.e. it has to be `@safe nothrow pure @nogc`.

It has to be as restrictive as declared, just like usual. It can never 
gain attributes, but can only remove them.

I still think that either the DIP as written or this alternative 
proposal would be better with an opt-in attribute such as @called.

-Steve


More information about the Digitalmars-d mailing list