Finding out which `with` clause failed in `else` block

Hi there,

recently I’ve seen an article about the with statement, just another one that describes how it can improve code readability. Ironically, the article ends with a long code snippet, neither concise nor readable, even though the author says some aspects were omitted ‘in favour of simplicity’. The snippet shows that the problem from this posts title can be solved like this:

with {:label1, result1} <- {:label1, expr1()},
     {:label2, result2} <- {:label2, expr2()} do
  do_something(result1, result2)
  {:label1, error} -> handle_error1(error)
  {:label2, error} -> handle_error2(error)

Since I once created a macro that helps do this in (I believe) a more pleasant way, I thought I’ll share it and maybe you like it. So it’s called withl and can be used as follows:

with label1: result1 <- expr1(),
     label2: result2 <- expr2() do
  do_something(result1, result2)
  label1: error -> handle_error1(error)
  label2: error -> handle_error2(error) # note that result1 is accessible here in case you need it

Our team uses this here and there for almost two years, so one day we put in into our bunch library. In the docs you can find a detailed description and more real-life examples. Let me know what you think :wink:


Looks like the tag feature in another library I’ve used that had a with-like construct before with existed, it was super useful, I wish Elixir would add it, and I like your version of it quite a lot actually however it seems it could be ambiguous if you are literally trying to match something like [{:label, _something}] or so, which is the issue there. The one I used in the past did it like this instead:

with @label1 result1 <- expr1(),
     @label2 result2 <- expr2() do
  do_something(result1, result2)
  @label1 error -> handle_error1(error)
  @label2 error -> handle_error2(error) # note that result1 is accessible here in case you need it

Which is less ambiguous (i’m unsure if it has ambiguity as it would not be valid syntax otherwise anyway).

The first argument of withl is a keyword list (btw that’s why it doesn’t have to deal with multiple arguments like with). Therefore, labels are always required, so your example would have to be something like

withl label: [{:label, _something}] <- #...

so there is no ambiguity anymore.

1 Like