Discussion Thread: DIP 1032--Function pointers and Delegate Parameters...--Community Review Round 1
H. S. Teoh
hsteoh at quickfur.ath.cx
Sat Apr 4 11:40:26 UTC 2020
On Sat, Apr 04, 2020 at 12:18:54AM -0700, Walter Bright via Digitalmars-d wrote:
> On 4/3/2020 5:09 PM, H. S. Teoh wrote:
> > IOW, it should be the *function* that inherits attributes from the
> > passed-in delegate, not the other way round as proposed by this DIP.
>
> Then you've got the problem of two delegate parameters with
> contradictory attributes.
That's not a problem, the function inherits the most permissive of the
two.
> But worse, lambdas get their attributes inferred. Pass a simple
> lambda, and suddenly the function it gets passed to must be pure? This
> isn't going to work.
I think you misunderstood. Or I explained poorly. What I have in mind
is the following.
To make it absolutely clear and unambiguous, I will assign a numerical
value to each attribute (this is just for explanatory purposes, it does
not imply actual implementation):
impure = 0, pure = 1
throws = 0, nothrow = 1
@system = 0, @safe/@trusted = 1
allocating = 0, @nogc = 1
Note that 0 is assigned to the most permissive value, and 1 is assigned
to the most restrictive.
For argument's sake, let's say a function's attributes is a bitmask
consisting of the above values, in the above order. So a function
marked pure nothrow @safe would correspond with the bitmask 0b1110.
If the function has one or more delegate parameters, then the
*effective* attribute set of a particular call to that function is the
bitwise AND of its own bitmask and the bitmask(s) of its delegate
argument(s).
For example, if the function is pure nothrow @safe, which corresponds to
the bitmask 0b1110, and it's called with an impure, but otherwise
nothrow, @safe, @nogc delegate, corresponding with the bitmask 0b0111,
then its effective attribute set would be 0b1110 & 0b0111 == 0x0110,
i.e., nothrow @safe.
If the same function called with two delegates, with the first one pure
@safe but throws and allocates (i.e., 0b1010) and the second impure and
throwing but @safe @nogc (i.e., 0b0011), then that function call behaves
as if the function's attribute bitmask is 0b1110 & 0b1010 & 0b0011 ==
0b0010, that is, @safe (but impure, throwing, and allocating).
IOW, the effective attributes of a function call is the least permissive
among the function itself and its delegate arguments.
T
--
Elegant or ugly code as well as fine or rude sentences have something in common: they don't depend on the language. -- Luca De Vitis
More information about the Digitalmars-d
mailing list