Proposal: Relax rules for 'pure'

Walter Bright newshound2 at digitalmars.com
Thu Sep 23 02:57:58 PDT 2010


Don wrote:
> Walter Bright wrote:
>> Steven Schveighoffer wrote:
>>>> A pure function also cannot modify any data via its parameters. In 
>>>> other words, its parameters must be transitively const.
>>>
>>> Yes, a strongly pure function must have those traits.
>>
>> No, that would be a weakly pure one.
> 
> You're operating with a different definition. There are actually three 
> levels. For extra clarity, let's split strongly-pure into two:
> 
> immutably pure == all parameters are immutable
> const pure == all parameters are const
> weakly pure == no access to globals
> 
> The interestingly thing is that an immutably pure function can safely 
> call a weakly-pure function. The only things the weakly pure function 
> will be able to change, are variables which are local to the immutably 
> pure functions.

Ok, I see your point now. It's a good one.

> 
> The *only* difference between weakly pure and immutably pure is the 
> guarantees which are made about the parameters. And this is already 
> covered by the const modifiers in the function signature. So that 
> concept doesn't actually need to be part of pure.
> 
> Note that this is possible only because immutable and const are 
> transitive. It wouldn't be possible with head-const, or logical const.
> Transitive const gives us an absolutely huge win.


More information about the Digitalmars-d mailing list