Another purity question

Timon Gehr timon.gehr at gmx.ch
Tue Jan 14 12:39:42 PST 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);
}



More information about the Digitalmars-d-learn mailing list