-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Pure functions #520
Comments
In practice it would be handy if calling "no side effects" function is visually distinct from an ordinary function call. E.g. by different highlighting in IDE, or like this:
|
See also #425. |
If a function uses C++ has similar problem, a template may throw or not, depending on a type. They added |
Is it worth reconsidering something like this proposal (perhaps as a calling convention --
|
This is an invitation to discussion as much as it is a proposal. While function purity known from traditional functional languages probably does not belong in Zig, I believe a weaker kind of purity is worth considering. Reasoning about a program's potential states is often overwhelming or impossible. With pure functions, we can make it much easier for the programmer to reason about the state space, and as a nice side-benefit, a new class of compiler optimizations become feasible (as far as I can see anyway).
More concretely, when I write C/C++ I often miss the ability to limit a function to only access passed arguments. Example (note
pure
qualifier beforefn
):Note that this particular formulation of purity allows changing referenced values (in contrast to functional languages).
Function purity in non-functional languages, was eloquently addressed some years back by John Carmack:
https://www.gamasutra.com/view/news/169296/Indepth_Functional_programming_in_C.php
The language D has some form of this purity. Personally, I like their notion of function purity though I admittedly do not have experience using it in practice. For reference and inspiration, here's a nice article about its design:
http://klickverbot.at/blog/2012/05/purity-in-d/
Advantages of pure functions:
I do not have much experience with language design and/or compiler authoring, so I am sorry if I am overlooking some obvious reason why this would be a bad idea. Anyway, I would love to hear your thoughts on this. Thanks.
The text was updated successfully, but these errors were encountered: