Proposal: Relax rules for 'pure'
Don
nospam at nospam.com
Wed Sep 22 14:19:59 PDT 2010
sclytrack wrote:
>> weaklypure void reverse(int[] x)
>> {
>> for(int i = 0; i * 2 < x.length; i++)
>> swap(x[i], x[$-1-i]);
>> }
>> pure int foo(const(int)[] x)
>> {
>> auto x2 = x.dup;
>> reverse(x2);
>> // do some calculation on x2
>> ...
>> return calculation;
>> }
>
> noglobal void reverse(MyClass x)
> {
> x.text = "";
> }
>
> So weakly-pure should not access global stuff. But that is harder
> to track by the compiler with mutable parameters.
Not really. You just need to disallow shared, __gshared in pure function
declarations.
> Or weakly-pure is only noglobal when called in pure routines, because of the
> immutable barrier.
> Unwritten contract, not checked by the compiler? (Like property getters shouldn't
> modify the "state" of the object.)
No. The contract is, that the function only has access to the parameters
you have given it. It depends _only_ on its parameters.
If you've made all parameters immutable or value types, it's strongly
pure.
These additional contracts come automatically from the type system.
More information about the Digitalmars-d
mailing list