Hacker News new | ask | show | jobs
by sergiotapia 2540 days ago
You need to do it this way, it's more idiomatic:

    with {:user_created?, {:ok, user}} <- create_user do
      # do something with `user`.
    else
      {:user_created?, {:error, errors}} -> 
        # something
      _ ->
        # some unhandled error
    end
And so on, use an atom on the fly to identify branches and error conditions. But honestly most of the time you worry about happy paths and Let It Crash.
1 comments

yeah this is kind of horrible once you get to high levels of nesting. in an imperative language you can return early in the error paths and your function will still be understandable. this doesn't work with a single return. you can easily have 6 guard statements in an imperative language and your function will be comprehensible try doing 6 levels of nesting in elixir and it will be a complete mess.
Maybe the “OK” library [1] could be useful for you.

I would also welcome a better built-in idiom for early returns.

[1] https://hexdocs.pm/ok/readme.html