Idea: partially pure functions

Bruno Medeiros brunodomedeiros+spam at com.gmail
Thu May 1 08:45:13 PDT 2008


Steven Schveighoffer wrote:
> 
> 
> So in the case of a partially pure function that takes a char array:
> 
> pure void f(char[] x) {...}
> 
> Let's say I have a non-pure function g:
> 
> void g()
> {
>     char[] v;
>     ...
>     f(v);
> }
> 
> In the ..., v can be set to local mutable heap data (via new char[x] or 
> whatever), or it could be set to point to a global mutable string.  How can 
> the compiler be sure without severe context analysis?  The point is, in 
> order to ensure context-free lexical analysis by the compiler, it must 
> disallow calling f from a non-pure function.
> 

Huh? You're mixing things up.
Yes, if g() is not pure, then the compiler will not check if v points to 
global data or not. Thus we cannot guarantee that calling "f(v)" will 
not change global state. But so what?
That doesn't mean we shouldn't allow it. It just means such call may 
have side-effects, and the compiler should threat that as a normal 
function call (make no particular pure optimizations). Thus "partially" 
pure: pure in some contexts, impure in other contexts.



-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list