digitalmars.D.learn - Another purity question
- bearophile (15/15) Jan 14 2014 Currently D refuses code like this:
- David Nadlinger (18/19) Jan 14 2014 In the general case, no:
- Meta (3/22) Jan 14 2014 Isn't this okay in the context of weak purity?
- Tobias Pankrath (3/22) Jan 14 2014 It's pure in the sense that it only modifies data passed to it?
- Timon Gehr (18/37) Jan 14 2014 int delegate()pure foo(const int[] a)pure{
- Timon Gehr (3/18) Jan 14 2014 Yes, it should.
- David Nadlinger (4/6) Jan 14 2014 And, of course, you are right. ;) I missed the analogy to member
- Timon Gehr (3/10) Jan 14 2014 Well, if it wasn't easy to fall into this trap (and this is mostly due
- bearophile (5/7) Jan 14 2014 Thank you. I have added my (probably redundant) test case to that
Currently D refuses code like this: void foo(const int[] a) { int bar() pure { return a[0]; } } void main() {} With two repeated error messages: test1.d(3): Error: pure nested function 'bar' cannot access mutable data 'a' test1.d(3): Error: pure nested function 'bar' cannot access mutable data 'a' But is it possible for D to see that bar function as pure? Bye, bearophile
Jan 14 2014
On Tuesday, 14 January 2014 at 19:50:10 UTC, bearophile wrote:But is it possible for D to see that bar function as pure?In the general case, no: --- auto foo(const int[] a) { int bar() { return a[0]; } return &bar; } void main() { int[3] a; auto dg = foo(a[]); assert(dg() == 0); a[0] = 1; assert(dg() == 1); } --- David
Jan 14 2014
On Tuesday, 14 January 2014 at 20:21:25 UTC, David Nadlinger wrote:On Tuesday, 14 January 2014 at 19:50:10 UTC, bearophile wrote:Isn't this okay in the context of weak purity?But is it possible for D to see that bar function as pure?In the general case, no: --- auto foo(const int[] a) { int bar() { return a[0]; } return &bar; } void main() { int[3] a; auto dg = foo(a[]); assert(dg() == 0); a[0] = 1; assert(dg() == 1); } --- David
Jan 14 2014
On Tuesday, 14 January 2014 at 20:21:25 UTC, David Nadlinger wrote:On Tuesday, 14 January 2014 at 19:50:10 UTC, bearophile wrote:It's pure in the sense that it only modifies data passed to it?But is it possible for D to see that bar function as pure?In the general case, no: --- auto foo(const int[] a) { int bar() { return a[0]; } return &bar; } void main() { int[3] a; auto dg = foo(a[]); assert(dg() == 0); a[0] = 1; assert(dg() == 1); } --- David
Jan 14 2014
On 01/14/2014 09:21 PM, David Nadlinger wrote:On Tuesday, 14 January 2014 at 19:50:10 UTC, bearophile wrote:int delegate()pure foo(const int[] a)pure{ struct S{ const int[] a; int bar()pure{ return a[0]; } } auto s=S(a); return &s.bar; } void main() { int[3] a; auto dg = foo(a[]); assert(dg() == 0); a[0] = 1; assert(dg() == 1); }But is it possible for D to see that bar function as pure?In the general case, no: --- auto foo(const int[] a) { int bar() { return a[0]; } return &bar; } void main() { int[3] a; auto dg = foo(a[]); assert(dg() == 0); a[0] = 1; assert(dg() == 1); } --- David
Jan 14 2014
On 01/14/2014 08:50 PM, bearophile wrote:Currently D refuses code like this: void foo(const int[] a) { int bar() pure { return a[0]; } } void main() {} With two repeated error messages: test1.d(3): Error: pure nested function 'bar' cannot access mutable data 'a' test1.d(3): Error: pure nested function 'bar' cannot access mutable data 'a' But is it possible for D to see that bar function as pure? Bye, bearophileYes, it should. https://d.puremagic.com/issues/show_bug.cgi?id=9148
Jan 14 2014
On Tuesday, 14 January 2014 at 20:36:43 UTC, Timon Gehr wrote:Yes, it should. https://d.puremagic.com/issues/show_bug.cgi?id=9148And, of course, you are right. ;) I missed the analogy to member functions w.r.t. the implicit context parameter. Shame on me. David
Jan 14 2014
On 01/14/2014 09:41 PM, David Nadlinger wrote:On Tuesday, 14 January 2014 at 20:36:43 UTC, Timon Gehr wrote:Well, if it wasn't easy to fall into this trap (and this is mostly due to terminology), DMD would actually behave correctly. :o)Yes, it should. https://d.puremagic.com/issues/show_bug.cgi?id=9148And, of course, you are right. ;) I missed the analogy to member functions w.r.t. the implicit context parameter. Shame on me. David
Jan 14 2014
Timon Gehr:Yes, it should. https://d.puremagic.com/issues/show_bug.cgi?id=9148Thank you. I have added my (probably redundant) test case to that issue. Bye, bearophile
Jan 14 2014