On 01/12/2014 11:49 AM, Peter Alexander wrote:
Also, I was under the impression a 'strongly pure' function's arguments
only need to be const, not necessarily immutable. Purity says something
about the transformation performed by the function, nothing about the
data
it operates on.
Why should all arguments need to be immutable?
You don't need immutable arguments for purity, just strong purity. It's
a stronger guarantee, more than normally guaranteed. Think of strong
purity as pure + referentially transparent.
Sorry, yes you're right, they only need to be const. And it is only if
you return a mutable value that the result becomes convertible to
immutable.
int* f(const(int)* x); // convertible
const(int)* f(const(int)* x); // not-convertible
...
(I assume you meant those to be pure.)
This is safe in the first instance because the result could not have
come from x due to x being const. In the second instance, the result
could have come from x, so it cannot be implicitly converted to immutable.
Well, currently things are supposed to be as follows:
1. A strongly pure callable is a pure callable whose parameter types
implicitly convert to immutable.
2. The result of a call to a strongly pure callable implicitly converts
to immutable.
The following vastly more general rule would still be sound and also
capture your case:
- The result of a call to a pure callable, where all arguments whose
corresponding parameter types are incompatible with the result type
implicitly convert to immutable (shared), implicitly converts to
immutable (shared). (Incompatibility should probably just be
incompatibility of qualifiers.)