Expanding the horizons of D purity

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Nov 1 10:19:51 PDT 2013


On Fri, Nov 01, 2013 at 12:26:43AM +0100, deadalnix wrote:
> I think you take it the wrong way. Weak purity have some guarantee
> in itself, like you know it won't reach shared data unless you pass
> them explicitly, do not touch anything static, etc . . .

This still holds. Passing a delegate that closes over shared data is
equal to passing in a reference to that data as argument. It's
compatible with the current definition of weak purity.


> You are basically addressing 2 more general problems here. Both are
> real and larger than the case you are considering.
> 
> The first one is delegate purity and context type qualifier. As you
> mention, a delegate's context is simply some extra data that get
> passed to the delegate as ARGUMENT. So the delegate must be able to
> mutate this while being pure.
> 
> That is the first thing : pure delegate must be able to mutate their
> context.

There is also the case where you want toString() to work for *both* pure
and impure code. By allowing it to call any delegate, we're essentially
saying "the body of this function is pure, depending on the purity of
the call to the delegate". So you can pass in a completely impure
delegate and it will behave like an impure function, but the trick here
is that (1) impure delegates cannot be created within strongly pure
functions (i.e., delegates that cause side-effects to appear outside the
strongly pure function), and (2) all delegates created within strongly
pure functions will only produce pure behaviour with the function that
invokes the delegate. IOW, when called from pure code, the function is
actually pure, even if it's impure when called from impure code.


> The second one is the inout problem. Qualifier in output may reflect
> the one in inputs. inout solve this for some type qualifiers, but
> sometime is ambiguous and does nothing for the problem at large.

I'm not sure what inout has to do with purity. It's another can o' worms
that I'm avoiding to open, at the moment. :P


T

-- 
Your inconsistency is the only consistent thing about you! -- KD


More information about the Digitalmars-d mailing list