Purity
Don
nospam at nospam.com
Fri Dec 17 22:27:40 PST 2010
Steven Schveighoffer wrote:
> On Fri, 17 Dec 2010 09:39:19 -0500, Don <nospam at nospam.com> wrote:
>
>> A function which has immutably pure parameters can undergo *some*
>> optimisation, even if the return value is a mutable pointer.
>> For example, if the parameters are identical for both calls, you can
>> do a deepdup of the first return value instead of calling the function
>> again.
>
> Yes, I agree with this. However, I still believe statement 1 is still
> correct, because you really have just introduced a third class of pure
> functions that I was not aware of :)
>
> So we have:
>
> weak-pure functions : functions which can accept and return any type of
> values, but can only call pure functions and cannot access global state.
> middle-pure ? functions : weak-pure functions which accept only
> immutable or implicitly convertable to immutable values, but returns a
> mutable reference.
> strong-pure funtions : pure functions which accept and return only
> immutable or implicitly convertible to immutable values.
>
> There actually could be another class, const-pure functions: pure
> functions which accept and return only const or implicitly convertible
> to const values.
The problem with these ones, is there could be aliasing between the
input and the output. Do they have any interesting properties?
Another interesting one is a function with all-const parameters, that
returns a mutable reference that has only mutable or immutable members.
(ie, nothing const). Like the one you've called middle-pure, this
returns a unique result, because you're guaranteed there is no aliasing.
> I suspect the compiler will need to classify things as strong or weak
> pure, and store various attributes on weak-pure functions to see what
> optimizations can be had.
The compiler does that already.
More information about the Digitalmars-d
mailing list