2 ms·
It would be even better if it was a linting/compile or at least a runtime error to assign a variable to the result of one of those functions. Python let's you
by coldtea 8y ago
It would be even better if it was a linting/compile or at least a runtime error to assign a variable to the result of one of those functions.
Python let's you do it, and you get "None" as the result, which makes such mistakes slower to discover.
This would require some way to mark a function as returning or void.
- nothrabannosir 8y agoA clean solution would be arity of return values, optionally with a ≥ constraint on the original number of returned values vs bound variable names. E.g. a function returning two values can be bound to zero, one, or two variables, but not three. A function returning 1 can be bound to only 1 or none, and if you return no values, you cannot be bound to anything. Aside: it is a common misconception that Python has multiple return values: it does not. It can return a tuple, which can be automatically destructured into multiple values: a, b = (1, 2) # this could have been a function and it has syntactic sugar for tuples which allows leaving off the parantheses: >>> 1, 2 (1, 2) Which, combined, makes it look like multiple return values: def foo(): return 1, 2 but, it really isn't: >>> foo() (1, 2) >>> type(foo()) <type 'tuple'> so this would be hard to achieve in Python. :(
- coldtea 8y agoWell, couldn't it be like "no return inside the function body" --> no LHS assignment allowed?
- nothrabannosir 8y agoSure :) but it would require a deep change in Python language semantics, without which this isn't possible. You'd have to carry that information in the AST somehow. From a call site, a function object is just a an object with certain properties. "Did it have a return or not?" is not information which exists on that object. Only the return value is (when you call it), and if it is None, you don't know if that came from "return None" or from the default behaviour. Python could enrich the function object to include that information, and it would essentially be "supporting 0 or 1 return values", instead of "n return values". But adding that no-assignment rule would be backwards incompatible. At that point, might as well go all the way and support any number return values :)
- protonfish 8y agoI 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.