To help LDC/GDC

Artur Skawina art.08.09 at gmail.com
Tue Apr 9 10:54:39 PDT 2013


On 04/09/13 15:22, Timon Gehr wrote:
> On 04/09/2013 03:18 PM, Artur Skawina wrote:
>> On 04/09/13 13:47, Simen Kjærås wrote:
>>> On Tue, 09 Apr 2013 13:10:16 +0200, Artur Skawina <art.08.09 at gmail.com> wrote:
>>>
>>>> A function that both directly depends on global mutable state (and
>>>> modifies it) can hardly be called pure. Can you (anybody) give a
>>>> D "pure" definition that allows for the program that I've posted
>>>> and still makes "pure" useful?
>>>
>>> Functions that are pure may only mutate mutable state explicitly passed
>>> to them, or created within.
>>
>>     struct S;
>>     int f(S* p) pure;
>>     S* n();
>>
>>     int g = 0;
>>
>>     int main() {
>>        S* s = n();
>>        f(s);
>>        return g;
>>     }
>>
>> Is main() guaranteed to return '0'?
> 
> No. What is the point?

I'm exploring the D purity definition given above, which is either
incorrect or extremely misleading. My example was bad though, as it
had too many unstated assumptions. A better one would have been:

   struct S;
   int g;
   int f(S* p) pure;
   int h(S* s) { g = 0; f(s); return g; }
   // Is 'h' guaranteed to return '0'?

Obviously, the answer is no.
Yet this will be expected by anyone not aware of how "pure" works in D.
Adding "const" would only change the R/W dep to a R/O one (IOW 'f' could
still return different values depending on 'g').

artur


More information about the Digitalmars-d mailing list