Idea: partially pure functions

Steven Schveighoffer schveiguy at yahoo.com
Thu May 1 07:42:40 PDT 2008


"Bruno Medeiros" wrote
> I state that if you have 'pure' implemented (in a sensible manner), it 
> takes zero effort to implement partial/contextual pure.
>
> The definition of partial pure functions requires no extra effort from the 
> compiler to check. (The *same* check for normal pure functions is made: 
> that no external state is accessed unless invariant)
>
> The usage of partial pure functions also requires no extra effort. Inside 
> a pure function body, the compiler already has to check if no external, 
> non-invariant variables are accessed, like this:
>
>   Foo globalVar;
>
>   pure void func(...) {
>     localVar = globalVar + 1; // not allowed
>
> then just apply this same check on *any* expression, including function 
> call expressions:
>
>   partiallyPureFunction(localVar, globalVar); // not allowed
>   partiallyPureFunction(localVar, localVar2); // but this is allowed

What if localVar is a pointer to global data, or transitively contains a 
pointer to global data?  There is no way to know whether localVar is a 
pointer to global data or 'contained' heap data without lots of context 
checking.  If partially pure functions with mutable parameters were allowed, 
I'd say it's a requirement that you can only call them from partially or 
fully pure functions, as local pointers from those functions are guaranteed 
to be contained within the function.

-Steve 





More information about the Digitalmars-d mailing list