[Issue 8185] Pure functions and pointers

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 4 03:17:25 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8185



--- Comment #26 from Jonathan M Davis <jmdavisProg at gmx.com> 2012-06-04 03:19:22 PDT ---
I'd actually argue that the line "Pure functions are functions that produce the
same result for the same arguments" should be removed from the spec.
Ostensibly, yes. The same arguments will result in the same result, but that
doesn't really have anything to do with how pure is defined. It's more like
it's a side effect of the fact that you can't access global mutable state. It's
true that the compiler will elide additional function calls within an
expression in cases where the same function is called multiple times with the
same arguments and the compiler can guarantee that the result will be the same,
but that's arguably an implementation detail of the optimizer.

While the origin and original motivation for pure in D was to enable
optimizations based on functional purity (multiple calls to the same function
with the same arguments are guaranteed to have the same results), that's not
really what pure in D does now, and talking about that clouds the issue
something awful, as this bug report demonstrates.

Pure means solely that the function cannot access any global or static
variables which can be mutated either directly or indirectly once instantiated
and that the function cannot call any other functions which are not pure. That
enables the whole "same result for the same arguments" thing, but it does _not_
mean that in and of itself. The simple fact that an argument could have a
function on it which returns the value of a mutable global variable without
that variable being part of its state at all negates that.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list