5 ms·
I don't really like this article cuz: - I like &Map.get/3 over the some_map[:some_key] syntax - &with/1 can be hellza confusing in many case if you're calling
by devoutsalsa 5y ago
I don't really like this article cuz:
- I like &Map.get/3 over the some_map[:some_key] syntax
- &with/1 can be hellza confusing in many case if you're calling a bunch of functions, and any ol' one of them can return an error message (e.g. where the heck did `{:error, :bad_arg}` come from?!
- piping into case statements is awesome, and I'll only stop doing it when people complain about it
- higher order functions are useful, and I don't see a problem using one's judgement as need on when to use them
- In the cases where using &with/1 is actually useful, so is using the else block if you really need to
...
Yeah, I just don't like this article's recommendations.
- cormacrelf 5y agoI’ve never used Elixir, but when I saw piping into case statements I was confused as to why it’s appearing on a ‘do not use’ list. It does seem awesome! The key thing for error handling is to be able to do it inline, without introducing nesting, like those withs are doing. Maybe you don’t get that with case either. Does elixir have a return statement?
- pipingdog 5y agoElixir does not have a return keyword.
- jkhdigital 5y agoElixir, like Erlang, is functional so there are no statements. Everything is an expression, which evaluates to some value when executed. The “return” value is just the value of the last expression evaluated in the function call.
- cormacrelf 5y agoIs there an equivalent of the Haskell Either monad & do-notation / Rust Result + the ? (try) operator?
- mcintyre1994 5y agoSort of, there’s a convention used throughout the standard library of returning {:ok, output} or {:err, error} or nil. That makes it really easy to compose functions/check for errors. The equivalent of do-notation would be with statements I think: https://elixirschool.com/en/lessons/basics/control-structures/#with https://elixirschool.com/en/lessons/basics/control-structure...
- cormacrelf 5y agoohhhhhhhhh, with statements do sequential . Yep. I'm on board with the original post now. Thanks!
- dnautics 5y agonot to be too nitpicky, but the article doesn't say don't use higher order functions; the article says don't wrap higher order functions into something that hides it higher-orderness.
- innocentoldguy 5y agoI agree with piping into case statements. It's awesome. You're a better person than me. I'll never stop doing it no matter how loudly people complain. :)
- tomjakubowski 5y ago> - &with/1 can be hellza confusing in many case if you're calling a bunch of functions, and any ol' one of them can return an error message (e.g. where the heck did `{:error, :bad_arg}` come from?! You agree with the article on this point: with is best used when you can fall through at any point without worrying about the specific error or contrary pattern.