[Issue 11046] [enh] Pure functions with default and out parameters are allowed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 15 14:04:08 PDT 2013


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


Jonathan M Davis <jmdavisProg at gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |jmdavisProg at gmx.com
         Resolution|                            |WONTFIX
            Summary|Pure functions with default |[enh] Pure functions with
                   |and out parameters are      |default and out parameters
                   |allowed                     |are allowed


--- Comment #3 from Jonathan M Davis <jmdavisProg at gmx.com> 2013-09-15 14:04:06 PDT ---
Of course, it makes sense to support such code. In such a case, it's a weakly
pure function rather than a strongly pure one. It can then be used by strongly
pure functions.

Weakly pure functions are functions which which cannot access global mutable
state (e.g. they can't access mutable global variables or have mutable static
variables in their bodies). By itself, that is _all_ that pure indicates. It
means _nothing_ about altering its arguments or returning the same value every
time or anything of the sort.

Strongly pure functions are pure functions whose parameters are all either
immutable or implicitly convertible to immutable. In such a case, the compiler
can guarantee that the function's arguments aren't mutated, and the compiler
can take advantage of that for optimization - e.g. calling foo only once in the
expression foo(2) * foo(2). Weakly pure functions cannot be optimized like
that, but they can be called from strongly pure functions (since they do not
affect anything outside of the strongly pure function and therefore do not
affect its guarantees), so they become very useful in writing strongly pure
functions.

So, pure in D really is not functional purity. Rather, it provides the building
blocks for function purity (and strongly pure functions are then functionally
pure).

In fact, the primary benefit of pure is arguably the fact that it guarantees
that the function does not access global mutable state rather than anything to
do with functional purity or optimizations.

-- 
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