4 ms·
> The benefits over an exception is as the caller i can pattern match on the result of this. so i could have this is in a function in a case Your example looks
by dahfizz 3y ago
> The benefits over an exception is as the caller i can pattern match on the result of this. so i could have this is in a function in a case
Your example looks just like exceptions to me, just with different keywords
try:
return with_example(fname)
except RecoverableError:
recover(fname)
except Exception as e:
raise e
- el_oni 3y agoIn the examples yes, because it's just a simple binary ok or error case but if you returned a list you can pattern match on an empty list, a single element list or a list that's longer. which you wouldn't do with exceptions in another language. that's the nice part about `with` that you can stop once you stop matching the pattern and return whatever you currently have, which in the list example may be a perfectly valid thing to return. with [value] <- Module.some_list_function(arg), # can return an empty list too [head | tail] = list <- Module.another_func(value), # can return a single element list longer_list <- Module.takes_multi_element_list(list) do longer_list end There are loads of other examples for uses of this, or you can just write in an additional clause for your function that handles the other case and pipe it down the line. It's about what makes sense for your domain. I may not be explaining this well so apologies for any confusion