3 ms·
In 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 lis
by el_oni 3y ago
In 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