confused about pure functions

markusle markus.lists at gmail.com
Sat Mar 17 11:27:06 PDT 2012


On Saturday, 17 March 2012 at 17:59:12 UTC, Andrei Alexandrescu 
wrote:
> 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

Thanks a lot for yours and Adam's detailed explanation. This 
makes much more sense now.


More information about the Digitalmars-d mailing list