Timeline for Does catching/throwing exceptions render an otherwise pure method impure?
Current License: CC BY-SA 3.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Oct 27, 2016 at 10:24 | comment | added | Idan Arye |
@AnoE But the code I've writted would ignore the exception for this imagined transformation. func(arg) would return the tuple (<junk>, TheException()) , and so ignoreThis will contain the tuple (<junk>, TheException()) , but since we never use ignoreThis the exception will have no effect on anything.
|
|
Oct 27, 2016 at 5:50 | comment | added | AnoE | Exactly... you would not have ignored the exception for this imagined transformation. I guess it's all a bit of a issue of semantics/definition, I just wanted to point out that the existence or occurence of exceptions does not necessarily void all notions of purity. | |
Oct 27, 2016 at 0:13 | comment | added | Idan Arye |
@AnoE If you would have written func that way, the block I have given could have been optimized away, because instead of unwinding the stack the thrown exception would have been encoded in ignoreThis , which we ignore. Unlike exceptions that unwind the stack, exception encoded in the return type do not violate purity - and that's why many functional languages prefer to use them.
|
|
Oct 26, 2016 at 22:56 | comment | added | AnoE |
Your first argument (until the "But...") seems unsound to me. The exception is part of the contract of the function. You can, conceptionally, rewrite any function as (value, exception) = func(arg) and remove the possibility of throwing an exception (in some languages and for some types of exceptions you actually need to list it with the definition, same as arguments or return value). Now it would be just as pure as before (provided it always returns a.k.a throws an exception given the same argument). Throwing an exception is not a side effect, if you use this interpretation.
|
|
Oct 26, 2016 at 19:24 | history | edited | Robert Harvey | CC BY-SA 3.0 |
added 11 characters in body
|
Oct 26, 2016 at 18:28 | comment | added | Rock Anthony Johnson | +1 For your suggested use of ArgumentException, and for your recommendation to not "throw exceptions from pure functions with the intention that they'll be catched". | |
Oct 26, 2016 at 17:58 | history | answered | Idan Arye | CC BY-SA 3.0 |