www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Another purity question

reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent reply "David Nadlinger" <code klickverbot.at> writes:
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
next sibling parent "Meta" <jared771 gmail.com> writes:
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:
 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
Isn't this okay in the context of weak purity?
Jan 14 2014
prev sibling next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
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:
 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
It's pure in the sense that it only modifies data passed to it?
Jan 14 2014
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 01/14/2014 09:21 PM, David Nadlinger wrote:
 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
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); }
Jan 14 2014
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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,
 bearophile
Yes, it should. https://d.puremagic.com/issues/show_bug.cgi?id=9148
Jan 14 2014
next sibling parent reply "David Nadlinger" <code klickverbot.at> writes:
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=9148
And, 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
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 01/14/2014 09:41 PM, David Nadlinger wrote:
 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=9148
And, of course, you are right. ;) I missed the analogy to member functions w.r.t. the implicit context parameter. Shame on me. David
Well, if it wasn't easy to fall into this trap (and this is mostly due to terminology), DMD would actually behave correctly. :o)
Jan 14 2014
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 Yes, it should.

 https://d.puremagic.com/issues/show_bug.cgi?id=9148
Thank you. I have added my (probably redundant) test case to that issue. Bye, bearophile
Jan 14 2014