Do pure functions solve the "return const" problems?

Christopher Wright dhasenan at gmail.com
Wed Apr 9 16:36:27 PDT 2008


Janice Caron wrote:
> On 07/04/2008, Christopher Wright <dhasenan at gmail.com> wrote:
>>  This already works. Why couldn't pure functions do this? You can't tell me.
>> You aren't arguing. You are just stating that the compiler can't do
>> something that it already does.
> 
> Allow me to demonstrate:
> 
>     int f(out int x) pure
>     {
>         x = 1;
>         return 0;
>     }
> 
>     int g(out int x) pure
>     {
>         x = 2;
>         return 0;
>     }
> 
>     int h() pure
>     {
>         int n;
>         int t = f(n) + g(n);
>         return n + t;
>     }
> 
> Question: What does h return?
> 
> The problem is, it depends on evaluation order. If f(n) is evaluated
> last, h() will return 1, but if g(n) is evalutated last, h() will
> return 2. Pure functions are not allowed to depend on evaluation
> order, therefore f() and g() cannot be pure - and hence, neither can
> h().
> 
> My conclusion is that you can't have out parameters in pure functions.

Yay! I got you to argue!

But you seem to be saying that, if you remove 'pure' from all those 
functions, the result will be undefined. Is this the case?



More information about the Digitalmars-d mailing list