Expanding the horizons of D purity

Timon Gehr timon.gehr at gmx.ch
Fri Nov 1 04:45:22 PDT 2013


On 11/01/2013 12:26 AM, deadalnix wrote:
> I think you take it the wrong way. Weak purity have some guarantee in
> itself, like you know it won't reach shared data

I assume you mean mutable shared data.

> unless you pass them explicitly, do not touch anything static,

Unless you pass it in explicitly.

> etc . . .

Consider this:

shared static int x;

auto bar(){
     class S{
	shared(int)* p;
	this(shared(int)* p){ this.p=p; }
         int member(int y)pure{
             return *p=y;
         }
     }
     auto s=new S(&x);
     return &s.member;
}

/+
auto bar_prime(){ // does the same thing. (but is rejected by DMD)
     return (y)pure=x=y;
}+/

auto foo(int delegate(int)pure dg, int x)pure{
     return dg(x);
}

void main(){
     auto a=bar(); // note: a is pure
     assert(x==0);
     foo(a,2); // note: foo is weakly pure
     assert(x==2);
     foo(a,3);
     assert(x==3);
}



More information about the Digitalmars-d mailing list