ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue May 30 07:54:13 PDT 2017
On 05/30/2017 11:12 AM, Rene Zwanenburg wrote:
> If malloc were marked as pure, wouldn't that mean it must return the
> same pointer every time you call it with the same size?
D's `pure` mostly means: "does not access mutable state, and does not do
There is never a requirement that a function must return the same value
for the same input. But a compiler is allowed to memoize the result of a
`pure` function when it has no mutable indirections in its parameter and
return types. Such a function is "strongly pure".
When there are mutable indirections, the function is "weakly pure".
Weakly pure functions are not assumed to be memoizable, but "weakly
pure" still has meaning:
* Can call weakly pure functions from strongly pure ones.
* When a weakly pure function has mutable indirections in the return
type but not in the parameters (like malloc), then it must be returning
freshly allocated memory. That means, the result cannot be referenced
from anywhere else. So it can be converted to const/immutable/shared
implicitly. The spec calls that a "pure factory function".
Repeating Biotronic's links, the spec and David Nadlinger's article are
the go-to resources for D's take on purity:
More information about the Digitalmars-d-learn