Thread overview
Another purity question
Jan 14, 2014
bearophile
Jan 14, 2014
David Nadlinger
Jan 14, 2014
Meta
Jan 14, 2014
Tobias Pankrath
Jan 14, 2014
Timon Gehr
Jan 14, 2014
Timon Gehr
Jan 14, 2014
David Nadlinger
Jan 14, 2014
Timon Gehr
Jan 14, 2014
bearophile
January 14, 2014
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
January 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
January 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:
>> 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?
January 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:
>> 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?

January 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,
> bearophile

Yes, it should.

https://d.puremagic.com/issues/show_bug.cgi?id=9148
January 14, 2014
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);
}

January 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=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
January 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:
>> 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)
January 14, 2014
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