Re: [SO] Modifing local class instance in pure function

2009-06-19 Thread bearophile
div0: > 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? Interesting question, I have tried the following cod

Re: [SO] Modifing local class instance in pure function

2009-06-19 Thread Steven Schveighoffer
bearophile Wrote: > Probably there are ways to solve the situation, using unique or something > else, but I think that way leads to C++-style madness (see the ridiculously > complex lambdas of the last C++, they look like a joke to me. Those designers > have missed the point that the essential

Re: [SO] Modifing local class instance in pure function

2009-06-19 Thread Steven Schveighoffer
div0 Wrote: > In your example you are returning values types, not objects as in the > original question. Arrays are not value types. Substitute int[] x with a pointer, same argument applies. -Steve

Re: [SO] Modifing local class instance in pure function

2009-06-19 Thread div0
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Steven Schveighoffer wrote: > On Thu, 18 Jun 2009 18:16:36 -0400, div0 > 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_programmin

Re: [SO] Modifing local class instance in pure function

2009-06-19 Thread bearophile
Steven Schveighoffer: > 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

Re: [SO] Modifing local class instance in pure function

2009-06-18 Thread Steven Schveighoffer
On Thu, 18 Jun 2009 18:16:36 -0400, div0 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 funct

Re: [SO] Modifing local class instance in pure function

2009-06-18 Thread BCS
Reply to div0, 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 Based on that, cons makes lisp not functional.

Re: [SO] Modifing local class instance in pure function

2009-06-18 Thread div0
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Jesse Phillips wrote: > http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0 > > class TestPure > { > string[] msg; > void addMsg( string s ) > { >msg ~= s; >} > }; > > pure TestPure run2() > { >TestPure t = new TestPur

[SO] Modifing local class instance in pure function

2009-06-17 Thread Jesse Phillips
http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0 class TestPure { string[] msg; void addMsg( string s ) { msg ~= s; } }; pure TestPure run2() { TestPure t = new TestPure(); t.addMsg("Test"); t.addMsg("this."); return t; } This doesn't work and the answer