[SO] Modifing local class instance in pure function

div0 div0 at users.sourceforge.net
Fri Jun 19 08:39:28 PDT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steven Schveighoffer wrote:
> On Thu, 18 Jun 2009 18:16:36 -0400, div0 <div0 at users.sourceforge.net>
> wrote:
> 
>> Pure functions are not allowed to alter global state.
>> That's what you doing when you create a new object.
>>
>> http://en.wikipedia.org/wiki/Functional_programming
>>
> 
> I once thought as you do.  You are wrong.  Memory allocation is a
> necessary function, and although it is inherently unpure, it must be
> allowed in pure functions.
> 
> The problem is that the function boundary is where purity is enforced,
> not inside.  For instance, basic programming 101, split common tasks
> into functions:
> 
> 
> int[] f()
> {
>    int[] x = new int[5];
>    foreach(i, ref n; x) n = i;
>    return x;
> }
> 
> int[] g()
> {
>    int[] x = new int[6];
>    foreach(i, ref n; x) n = i;
>    return x;
> }
> 
> f and g can be pure, because they do not have side effects.  However,
> consider:
> 
> void fill(int[] x)
> {
>    foreach(i, ref n; x) n = i;
> }
> 
> int[] f()
> {
>    int[] x = new int[5];
>    fill(x);
>    return x;
> }
> 
> Note that f is still pure, and fill is contextually pure, since in f's
> context, it is not altering external state.
> 
> However, we cannot mark fill as pure, because when called from a
> non-pure function, it could alter external state.  Therefore, fill
> cannot be called from f if f is pure.
> 
> This is similar to calling a method on an object:
> 
> C c = new C;
> c.method();
> 
> The method call translates to:
> 
> C.method(c);
> 
> The compiler cannot tell whether c is external state or not, just like
> it cannot tell that fill() can be contextually pure.  So it is not allowed.
> 
> I'm not sure what the solution is.  Something like 'unique' which would
> ensure that exclusive ownership of an object passes to the method would
> possibly allow passing mutable state into a pure function.  I'm unsure
> if there are any plans for something like this.
> 
> -Steve

Nuts I missed the main point of what I meant to say. Though I can see
the argument against the heap being global state is necessary or you
won't be able to do much with pure functions.

In your example you are returning values types, not objects as in the
original question.

Objects are defined to have an identity, so by returning a new object
from the function you are not getting the same result for the same inputs.

Does D relax the definition of pure to say a function may return an
equivalent object?

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFKO7EwT9LetA9XoXwRAkyiAJ0fUG20aU1IJSEa62yOCOTubvsmRgCgooGF
CXT3z2MqmhA+4wx8UW0Dyqc=
=uhBi
-----END PGP SIGNATURE-----


More information about the Digitalmars-d-learn mailing list