5 ms·
I feel like these types should be explicitly declared with two different keywords like "func" and "sub". Force one to return a value and prevent the other. This
by protonfish 8y ago
I feel like these types should be explicitly declared with two different keywords like "func" and "sub". Force one to return a value and prevent the other. This should shame people into writing functions without side effects and make reading code clearer.
- thestoicattack 8y agoAda uses "function" and "procedure." I think they've relaxed the rules slightly, but it used to be procedures have no return values and functions may not modify their arguments.
- coldtea 8y agoThis is probably orthogonal though. A function might not return anything and still have side-effects (and vice versa). That said, a "pure" modifier that ensure that no side-effect code is allowed inside a function would be good indeed. (There could be purity-levels as well, e.g. absolutely pure (doesn't even log or write something to a file or use a random number generator inside etc) vs "doesn't mutate program state" (which is a different kind of the same purity concept I think).
- klibertp 8y agoWell, that's how it works in Erlang... I'm only half-joking - the functions in Erlang are guaranteed to have no side-effects other than sending messages to other processes. So, if some computation never sends or receives a message, it is guaranteed to be side-effect free[1]. This is much weaker a guarantee than the function being pure, but in practice, it makes code reuse just a bit easier and writing stealthily mutating functions just a bit harder, which has real advantages. [1] Not really, though. There's the process dictionary, and there's no way to statically or dynamically mark a function as not using them. The use of it, however, is discouraged to the point that most popular Erlang books only mention it in passing close to the end, or not at all.
- waluigi 8y agoYeah, effect systems in general are a really cool idea that I wish we saw more of. Haskell and its derivatives are the only semi popular languages that have some sort of system, and those are just a few points in the design space that admittedly aren't perfect.