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

Avrina avrina12309412342 at gmail.com
Wed Jul 29 02:03:08 UTC 2020


On Wednesday, 29 July 2020 at 01:21:44 UTC, Walter Bright wrote:
> On 7/28/2020 4:00 PM, H. S. Teoh wrote:
>> This is a misunderstanding of what was proposed.  What was 
>> proposed is
>> that the compiler will treat such a call as if it were no 
>> longer pure
>> (@safe, nothrow, etc.). I.e., if the caller was marked pure, 
>> this would
>> trigger a compile error that it cannot call an impure 
>> function.  If the
>> caller was impure to begin with, then it doesn't matter anyway.
>
> Thanks for the clarification.
>
> But it still tricks the user into thinking the function is 
> pure, since it says right there it is pure. Pure isn't just an 
> attribute for the compiler, it's for the user as it offers a 
> guarantee about what the interface to a function is.
>
> Silently removing pure can also make the user think that a 
> function is thread-safe when it is not.
>
> Adding a feature that silently disables an explicitly placed 
> "pure" attribute is going to become a hated misfeature.
>
> I strongly oppose it.

It doesn't trick them. *It is* pure. The only thing that wouldn't 
be pure would be the delegate call. If you want to ensure it is 
pure, then you would just need to mark the delegate as pure as 
well. If you try and access a global you'd still get a compile 
error.

It'd be no different than what you can already do today with 
lazy. Just, it would actually error if the calling function was 
marked pure.

https://run.dlang.io/is/NNT4EC

import std.stdio;

__gshared int sp = 0;

int impureCall() {
	sp = 20;
     return sp;
}

pure int magic(lazy int value = impureCall()) {
	return value;
}

pure void parentCall() {
     magic();
}

void main() {
     writeln(sp);
     parentCall();
     writeln(sp);
}


----------------------------------------------

     Now with the suggested proposal:

__gshared int sp;

pure int foo(int delegate() value) {
     return value();
}

void test1() {
     foo(() { return sp; }); // ok
}

pure test2() {
     foo(()      { return sp; }); // error calling impure function 
foo()
     foo(() pure { return 0;  }); // ok
}




More information about the Digitalmars-d mailing list