Partially pure (Re: Fully transitive const is not necessary)

Leandro Lucarella llucax at gmail.com
Fri Apr 4 07:45:24 PDT 2008


Don Clugston, el  4 de abril a las 09:52 me escribiste:
> >The result of having run the function changes something outside the function, and that 
> >makes it impure.
> 
> foo() is definitely not pure. But bar() doesn't change anything outside bar().

Maybe foo should be marked as ... "local"? I.e. make some distinction
between methods that afect globals and methods that only modify locals.

> * foo() doesn't read or write anything other than its parameters.
> * The only parameters bar() provides to foo() are local.
> 
> >It does not matter that what's being changed is hidde behind the bushes in a small tin 
> >box.
> 
> I don't think that's what happening here. A 'partially pure' function like foo() can be 
> wrapped in a pure function.
> The question is whether the compiler can allow this, without foo() being marked in some 
> way.
> It works for CTFE, because in CTFE, the compiler has access to the source code; but that's 
> not necessarily true for run time functions.
> 
> This partial impurity is not transitive! (whereas accessing a global is transitive).

Well, this apply to functions too, what about:

class Foo { int i; }

void foo(Foo f)
{
	f.i = 5;
}

pure void bar()
{
	Foo f;
	foo(f);
}

bar() has no side effects either. Maybe foo should be marked... like "local".
But I think it should be much nicer if can be infered by the compiler, but
as you say it could be a problema if the source code is not available.

Maybe this kind of "annotations" can be automatically generated by the
compiler and put in the .di "headers"?

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Wake from your sleep,
the drying of your tears,
Today we escape, we escape.



More information about the Digitalmars-d mailing list