mat-hek

mat-hek

Membrane Core Team

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)
else
  {:label1, error} -> handle_error1(error)
  {:label2, error} -> handle_error2(error)
end

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)
else
  label1: error -> handle_error1(error)
  label2: error -> handle_error2(error) # note that result1 is accessible here in case you need it
end

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:

Most Liked

mat-hek

mat-hek

Membrane Core Team

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.

Where Next?

Popular in Discussions Top

cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
praveenperera
How We Replaced React with Phoenix By: Thought Bot
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |&gt; https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement