mat-hek

mat-hek

Membrane Core Team

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:

Showing Posts 1 to 2

OvermindDL1

OvermindDL1

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

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

mat-hek

mat-hek OP

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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews