Idea: partially pure functions

mort mortm at gmail.com
Thu Jul 31 18:46:30 PDT 2008


I'm not an expert on D, but it looks like a pretty nice language.  While reading about 'pure' recently, however, I found what appeared to be pretty major usability problems with the idea as it was presented:

- Pure functions can have local mutable state, but aren't allowed to factor the logic for manipulating that state out into functions (since such a function would be non-pure).

- Pure functions can't be applied to mutable data.  So you potentially need a mutable non-pure version of each pure function.

An elegant solution to both of these problems would seem to be the one proposed in this thread - get rid of the requirement that parameters be invariant.  The first problem above is solved by pure functions with mutable parameters, and the second by pure functions with const parameters.

Special optimizations that require purity and parameter invariance can always be done by creating a specialized version of each pure function that's invoked when the arguments are known to be invariant.  Let the compiler handle the duplication - don't make the programmer copy/paste.

Thanks





More information about the Digitalmars-d mailing list