Functional purity
The concept of functional purity was discussed briefly back in Chapter 7, Composing Functional Pipelines with Algorithms and Ranges. Now it's time for a quick introduction to functional purity as it is implemented in D. Consider the following function:
Vec2 add(Vec2 a, Vec2 b) pure { return Vec2(a.x + b.x, a.y + b.y); }
Assuming that Vec2
is a struct
and not a class
, then this function is as pure as a function can be. No global state is mutated, no parameters are mutated, and given multiple calls with the same arguments, the result will be the same every time. Note that it has been marked with the pure
attribute. With this, the compiler will produce an error if the function tries to modify any mutable static data (such as a module-scope ...
Get Learning D now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.