confused about pure functions

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Mar 17 10:59:09 PDT 2012


On 3/17/12 12:02 PM, markusle wrote:
> Using dmd 2.058 I can compile the following
>
> pure bool has_path(string paths[], string needle) {
>
> paths[0] = "bad";
>
> ... do something else ...
>
> return false;
> }
>
> and change the passed in array "paths". Isn't this a violation of
> has_path's pure contract? Shouldn't all pure function parameters
> be passed as "in" to avoid side effects. Sorry if I missed
> something obvious.

D's working definition of a pure function is "effect only depends on 
parameters". Mutating parameters does fit the definition.

This is less tight than other languages' definition, but we believe it's 
a sweet spot between reaping the modularity benefits of purity, and 
benefiting of the advantages of mutation.

Also, not all is lost. If you want to disallow parameter mutation, add 
"in" to them. That way, you only need to see the function's signature to 
draw a variety of interesting facts about it.


Andrei


More information about the Digitalmars-d mailing list