purity question

Biotronic via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 30 07:26:21 PDT 2017


On Tuesday, 30 May 2017 at 13:45:07 UTC, Rene Zwanenburg wrote:
> On Tuesday, 30 May 2017 at 11:34:52 UTC, ketmar wrote:
>>> If malloc were marked as pure, wouldn't that mean it must 
>>> return the same pointer every time you call it with the same 
>>> size?
>>
>> of course. but D "pure" is not what other world knows as 
>> "pure". we love to mess with words.
>
> Well, there's the ability to modify non-const reference 
> parameters from a pure function, but that's not applicable to 
> malloc. Are there any other special rules?

The rules[0] are:

0) Can't call functions not marked pure.
1) Can't touch anything mutable that's not explicitly passed to 
the pure function.
1b) Except GC internal state - i.e. memory can be allocated via 
the GC.
1c) And floating-point exception flags and modes.
2) Can't do I/O (can be seen as a special case of 1 and 0).

There's a few more details, but that's the important stuff.

For a good article on the subject, I recommend David Nadlinger's 
Purity in D:

http://klickverbot.at/blog/2012/05/purity-in-d/

[0]: https://dlang.org/spec/function.html#pure-functions


More information about the Digitalmars-d-learn mailing list