Do pure functions solve the "return const" problems?

Steven Schveighoffer schveiguy at yahoo.com
Wed Apr 2 12:34:43 PDT 2008


"Janice Caron" wrote
> On 02/04/2008, Yigal Chripun wrote:
>>  1) since the purpose of pure functions as I understand it is thread-safe
>>  functions,
>
> It isn't. A pure function is a function with no side effects.
>
>> than why should we care if they have side affects
>
> See above. Knowing that a function f is pure allows the compiler to
> make optimisations it cannot make with non-pure functions. For
> example:
>
>    x = f() + f();
>
> can be optimised to
>
>   x = 2 * f();
>
> thereby eliminating one entire function call. This makes your code go 
> faster.
>
> Pure functions also happen to be thread-safe, but not all thread-safe
> functions are pure.

That was my impression too, but read Bill's post closely.  What he is saying 
is that pure functions do not need invariant parameters, which means they 
are subject to thread issues.

If pure functions are not required to take invariant parameters, then they 
cannot be guaranteed to be thread safe.

-Steve 





More information about the Digitalmars-d mailing list