Purity with references and pointers

Simen kjaeraas simen.kjaras at gmail.com
Sat Sep 18 17:33:21 PDT 2010


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

> If a pure function takes a reference/pointer, does that state that the  
> result of
> the function will be the same on two calls to it if the  
> reference/pointer points
> to the same data in both cases or if the data itself is unchanged?
>
> If it's a matter of pointing to the same data, then that could be  
> horribly
> broken. That would mean that as long as I pased in the same reference,  
> the
> compiler could cache the result but that the actual result of the  
> function could
> have and should have been different for each call because the object  
> pointed to
> was altered.

"[A] pure function [...] has parameters that are all immutable or are
implicitly convertible to immutable" [1]

This implies that any pointer passed to a pure function must point
to immutable data. This means the pointed-to data can not change
between two calls to the function.


[1]: http://digitalmars.com/d/2.0/function.html#pure-functions
-- 
Simen


More information about the Digitalmars-d-learn mailing list