Pure Contract bug?

Timon Gehr timon.gehr at gmx.ch
Sat Feb 4 15:24:01 PST 2012


On 02/05/2012 12:15 AM, Era Scarecrow wrote:
>> Probably the restriction was lifted after TDPL was out.
>
>
>> Yes. The compiler will only reorder/run in parallel/optimize if it is
>> safe (not changing execution semantics). Pure can be used to prove
>> that certain optimizations are safe. If a pure function only takes
>> const or immutable arguments, the compiler has more guarantees and can
>> do more things. If a pure function takes mutable arguments, it can be
>> used as a component of other pure functions (important!), but the kind
>> of optimizations that can be performed directly on them are a lot more
>> limited.
>>
>> 'Pure' means that the behavior of the function does not change between
>> invocations with the same/equivalent arguments. This can include
>> mutating actions on the arguments, if those are typed mutable.
>
>
> Even if you changed the signature of the pure function to 'pure int
> squaredPlus(immutable int);' you'd have the same problem; Because the
> int argument it receives is a copy so it won't matter if it was mutable
> or not. (If it were an object, then it would be more enforced).
>
> I'll refer to the language specs to see if I can find an answer on this,
> but it feels wrong allowing access to 'this' on mutable data; I thought
> it could only mutate it's own data in regards to local variables and
> arguments it owned.

the signature I meant looks like

pure int squaredPlus(int)immutable;


More information about the Digitalmars-d-learn mailing list