Idea: partially pure functions

Don nospam at nospam.com.au
Wed Apr 30 02:41:04 PDT 2008


Bruno Medeiros wrote:
> I want to conceptualize an idea that has been briefly mentioned in the 
> previous pure discussions (by Don I think?), but has not been explicitly 
> brought into attention.
> 
> As we know, the current definition of pure functions (according to 
> http://www.digitalmars.com/d/2.0/accu-functional.pdf) is that they can 
> only access invariant, or local mutable data. This means that all of the 
> function's parameters have to be invariant.
> 
> The idea is: let's also allow pure functions to have (and access) 
> non-invariant parameters. These won't be normal pure functions, but 
> instead "partially" pure functions. Their semantics are: the function 
> promises not to change any external/global data, except for the 
> parameters that are not invariant. Example:

The first case to consider is nested functions.

pure int func(int x)
{
    int var=0;
    int localfunc(int y) { ++var; return x*var;  }
    int localfunc2(int y) { return y; }
    return localfunc(x)*localfunc(x+1)+localfunc2(x);
}

localfunc() is definitely not pure. localfunc2() is not marked as pure.
But I think you can make a pretty decent argument that func() is pure.

But Walter's already said that pure functions will start out very 
restricted, and the rules will be relaxed over time. So it's not really 
worth worrying about the rules right now.



More information about the Digitalmars-d mailing list