New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OK.with else pattern matching #10
Comments
I haven't yet implemented an else part of with. Although it shouldn't be hard. I think that when I do I will probably be strict such that it only works with however at the moment you can do the following. OK.with do
a <- func1()
b <- func2()
c <- func3(a, b)
end
|> case do
{:ok, value} ->
value
{:error, _reason} ->
its_ok_ill_return_a_default_value
end admittedly a bit messy. but I have used it a few times and it should refactor nicely when the else option arrives |
@CrowdHailer Thanks for the quick response 👍 I also am very strict (as strict as I can be about anything 😉) about returning I agree that the piped case statement is a bit messy, as I've seen that used in code before the But I just think it's so awesome the noise reduction! Ah well. I'll be looking forward to seeing what you come up with. Thanks 😄 |
I'm 90% sure the API will look like OK.with do
a <- func1()
b <- func2()
func3(a, b)
else
:reason1 ->
{:ok, :actually_ok_have_a_default}
:reason2 ->
{:error, :still_an_error_but_i_have_more_info_that_i_want_to_add_here}
end Unless there are any better suggestions. I'm pretty busy at the moment so will see when can get round to it. |
I'm not saying I have any better suggestions, I'm just talking this out! I really like your approach and your reasoning, and I'm just not sure on this one aspect. I totally understand about being busy on multiple fronts. 😅 As for this though, just to be explicit, in your example it would be a tuple I am still relatively new to the functional programming world, but one of the main fundamentals I've read quite a bit is that you don't want to throw an exception (unless it's actually exceptional). So this is what my default error handling code looks like:
Basically, I want to get it into the format of It's just this last case that I'm talking about handling. Would you be able to do this coercion yourself in the block do you think? (Because I think it should be apparent that I agree with you on the |
yest quite right on That is an interesting question. I don't normally wrap in the do block but only use functions that follow the pattern perfectly. I don't want to pollute
My solution is to use wrapped functions in the block OK.with do
a <- fetch_from_map(map, "a")
b <- fetch_from_map(map, "b")
# something with a & b
end
def fetch_from_map(map, key)
case Map.fetch(map, key) do
{:ok, v} ->
{:ok, v}
:error ->
{:error, :not_found_in_map}
end
end However if I make any mistakes then it does blow up with not very helpful messages so handling the case you are talking about might be good. OK.with do
:just_an_atom
else
%OK.UnexpectedReturnValue{value: garbage} ->
IO.inspect(garbage)
end |
Yes, I think we're pretty much in agreement on all points. I do think it's useful though to have the catchall garbage handler, and if you're able to code that into the macro itself so that I don't need to see that kind of noise - I think that would just be one more noise-reducing feature when using |
Just opened PR #11 to implement else |
closed |
I am just now starting to look at incorporating the
OK.with
block, and I really like the reduction of noise it gives, but I am unsure how to handle theelse
block. In my usage, 95% of the time I use a default error handler like so:I try to make all of my errors return in the form of
{:error, reason}
, but ya never know right. Sometimes I need to pattern match on other errors.Are you working on something parallel to elixir's
else
pattern matching inwith
statements, or is this functionality already implemented?The text was updated successfully, but these errors were encountered: