fceruti

fceruti

Happy Macro Monday to everyone!

I was wonder what kinds of crafty macros you think are awesome and have come to rely upon.

I’ll go first:

Sigil m


  defmacro sigil_m({:<<>>, _line, [string]}, []) do
    spec =
      string
      |> String.split()
      |> Stream.map(&String.to_atom/1)
      |> Enum.map(&{&1, {&1, [], nil}})

    {:%{}, [], spec}
  end

This one I copy-pasted from the web (can’t remember the origin) and it’s truly a killer in readability and speed of development.

I use it in many places, but on tests & liveview really shines:

Tests

test "happy path", ~%{user: user, project: project, task_1: task_1, task_2: task_2} do
test "happy path", ~m{user project task_1 task_2} do

Liveview

%{current_user: current_user,  project: project} = socket.assigns
~m{current_user project} = socket.assigns

Maybe Case

This one is “my own creation” (quotes apply as I received tons of help).

Think tap/1 meets case.

  defmacro maybe_case(prev, do: block) do
    quote do
      prev = unquote(prev)

      case prev do
        unquote(block ++ [{:->, [], [[{:_, [], nil}], nil]}])
      end

      prev
    end
  end

The use I have for this is the following. In my context layer I have all this functions such as schedule_task, unschedule_task etc, which in principle all look something like this:

Multi.new()
|> Multi.run(...)
|> Multi.run(...)
|> Multi.run(...)
|> Repo.transaction()
|> case do
   {:ok, %{action: ...}} -> {:ok, ...}
   {:ok, %{action_2: ...}} -> {:ok, ...}
   {:error, %{action: ...}} -> {:error, ...}
end

That’s great until you want to introduce side effects like logging and pubsubing. I could of course capture the result of the transaction, make conditional statements for publish & log and then shape the response.

results = Multi.new()
  |> Multi.run(...)
  |> Multi.run(...)
  |> Multi.run(...)
  |> Repo.transaction()

case results do
  {:ok, _, _} -> PubSub.publish_change()
  {:error, _, _} -> Logger.error()
end

case results do
   {:ok, %{action: ...}} -> {:ok, ...}
   {:ok, %{action_2: ...}} -> {:ok, ...}
   {:error, %{action: ...}} -> {:error, ...}
end

But being a pipe-man myself, I decided I needed my code to look like this:

Multi.new()
|> Multi.run(...)
|> Multi.run(...)
|> Multi.run(...)
|> Repo.transaction()
|> maybe_case do
  {:ok, _} -> PubSubs.publish_change(...)
  {:error, _} -> Logger.error(...)
end
|> case do
   {:ok, %{action: ...}} -> {:ok, ...}
   {:ok, %{action_2: ...}} -> {:ok, ...}
   {:error, %{action: ...}} -> {:error, ...}
end

Now it’s your turn!

Showing Posts 1 to 1

Nicd

Nicd

I don’t like macros. They’re obscure and inscrutable and irritating, and they get everywhere.

That said, I have two macros that I use to add type information to my code, my typed struct and typed Ecto schema macros. The latter is ugly and incomplete, but the former I can link for you: lib/geo_therminator/typed_struct.ex · 8fa72cbfa5b0402c46c53e13e5e994bf5a390f89 · Mikko Ahlroth / GeoTherminator · GitLab

I use it like this then:

defmodule Register do
  deftypedstruct(%{
    register_name: String.t(),
    register_value: {integer(), 0},
    timestamp: DateTime.t()
  })
end

It defines the struct, a @type t, and @enforce_keys for me all in one go. I copy it to every new project I start.

— 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