3 ms·
given a function fn that can fail, it will return a result and an error e.g. result, error = fn(...) calling this function should yield to the caller two
by preseinger 3y ago
given a function fn that can fail, it will return a result and an error e.g.
result, error = fn(...)
calling this function should yield to the caller two possibilities, somehow: a success value _or_ a failure error
the important thing is that in both cases, the control flow is visible in the source code as written
result, error = fn(...)
if there was an error, ...
if it was successful, ...
when an expression fails, you want to see the consequence in-line
the success path and the failure path are equally important
- andrewjf 3y agoYeah, but you're still relying on the programmer to do it correctly which is nothing but false hope.
- preseinger 3y agono, it isn't
- za3faran 3y agoNothing is preventing the code from returning both at the same time. I've seen code (including in the standard library) that returns both an error and a return value. In a language with disjoint unions, such cases would be encoded properly.
- preseinger 3y agoconvention prevents it and in the case where returning both is OK, then documentation makes that clear this is not difficult
- aniforprez 3y agoConvention in no way prevents anything. Convention is simply that. People are free to not follow convention when nothing is enforcing it. You frequently see juniors, who may be brand new to the language, making mistakes with conventions. If I'm supposed to depend on the vagaries of some accepted standard that is only documented in text then it is less than useless in the real world
- preseinger 3y agoif we say a language "addresses" a given concern, is it necessary that this is accomplished in the compiler, and that the rules for that concern, whatever they are, are enforced at compile-time? (spoiler: no)
- aniforprez 3y agospoiler: yes If the language "addresses" it by convention then it is not addressing it at a language level at all
- preseinger 3y ago"the language level" is not only what is defined and enforced by the compiler but i'm sure i won't convince you of anything here, so good luck to you
- za3faran 3y agoWe all know that documentation always 100% matches the behavior of the code :-)