When should pure function modify parameters?
Alex Bryan
abryancs at gmail.com
Tue Nov 18 21:32:57 UTC 2025
On Monday, 17 November 2025 at 14:46:06 UTC, Brother Bill wrote:
> Pure functions in Functional Programming provide the same
> result when provided the same arguments.
>
> They are not allowed to read or write to system resources such
> as file system, console, clock, network, etc. In addition,
> they may not read/write to module level variables.
>
> In D, they are allowed to mutate parameters which seems to
> violate purity. Why did D make this choice and when to best
> exploit this architectural decision.
>
> Also, would you agree that not mutating parameters to have
> "true" purity would be preferred?
There's a separate mechanism for describing the immutability of
parameters, and that's by using `const`/`immutable`/`in` for each
parameter. The `pure` keyword for me, covers the rest of the
purity of the function (globals). You could argue that `pure`
should enforce `const` or `immutable` for each parameter, but I
don't think that's a good idea as parameters are commonly used to
return values and I think marking a function that returns values
in one or more parameters `pure` to enforce that the function
doesn't change any globals still has value
More information about the Digitalmars-d-learn
mailing list