mat-hek
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 ![]()
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Looks like the
tagfeature in another library I’ve used that had a with-like construct beforewithexisted, 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:Which is less ambiguous (i’m unsure if it has ambiguity as it would not be valid syntax otherwise anyway).
mat-hek
The first argument of
withlis a keyword list (btw that’s why it doesn’t have to deal with multiple arguments likewith). Therefore, labels are always required, so your example would have to be something likeso there is no ambiguity anymore.