Partially pure (Re: Fully transitive const is not necessary)
Walter Bright
newshound1 at digitalmars.com
Thu Apr 3 22:36:42 PDT 2008
Don Clugston wrote:
> Walter Bright wrote:
>> A function is pure or it isn't, there really isn't any room for
>> "unstable" purity, or the point of purity is lost.
>
> I think there is. A function which accesses a global is inherently
> impure. But, a function which takes a non-invariant argument, can in
> theory safely be called from inside a pure function, provided any
> non-invariant arguments are local variables, or invariant.
>
> eg.
>
> class C
> {
> int numcalls;
> this() { numcalls=0; }
> void foo() { ++numcalls; } // Not pure - but no global side effects.
> }
>
> pure int bar(int x)
> {
> C c = new C;
> for (int i=0; i<x; ++i) c.foo();
> return c.numcalls;
> }
>
> Is bar() pure or not?
I think it is pure, although it might be difficult for the compiler to
detect it. Like for CTFE, I think the compiler should start out rather
restrictive about what it regards as pure, and over time expand it as it
gets more capable.
More information about the Digitalmars-d
mailing list