[Issue 8185] Pure functions and pointers

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jun 2 03:27:08 PDT 2012


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



--- Comment #3 from Denis Shelomovskij <verylonglogin.reg at gmail.com> 2012-06-02 14:29:01 MSD ---
(In reply to comment #1)
> The current behavior is by design, and perfectly fine – note that `pure` in D
> just means that a function doesn't access global (mutable) state. A pointer
> somewhere isn't a problem either, since the caller must have obtained the
> address from somewhere, and if it was indeed from global state, the calling
> code couldn't be pure.

OK. Looks like everything works but I don't understand how. So could you please
answer the question (read this to the end).

According to http://dlang.org/function.html#pure-functions
> Pure functions are functions that produce the same result for the same arguments.

And my original question is
> The Question: What exactly does these pure functions consider as `argument
value` and as `returned value`?

Illustration:
---
int f(in int* p) pure;

void g()
{
    auto arr = new int[5];
    auto res = f(arr.ptr);

    assert(res == f(arr.ptr));

    assert(res == f(arr.ptr + 1)); // *p isn't changed

    arr[1] = 7;
    assert(res == f(arr.ptr)); // neither p nor *p is changed

    arr[0] = 7;
    assert(res == f(arr.ptr)); // p isn't changed
}
---
Which asserts must pass?

The second assert is here according to
http://klickverbot.at/blog/2012/05/purity-in-d/  (yes, it's "Indirections in
the Return Type?" section, but sentences looks general and I think it can be
treated this way):
> The first essential point are addresses, respectively the definition of equality applied when considering referential transparency. In functional languages, the actual memory address that some value resides at is usually of little to no importance. D being a system programming language, however, exposes this concept.

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