TIL: Short notation for pattern-matching on any struct in Elixir

by Paweł Świątkowski
27 Jan 2026

When I want to pattern match against any struct in Elixir, I would usually do this:

iex(2)> %{__struct__: _} = %Order{id: 10}
%Order{id: 10}
iex(3)> %{__struct__: _} = %{id: 10}
** (MatchError) no match of right hand side value:

    %{id: 10}

    (stdlib 7.1) erl_eval.erl:672: :erl_eval.expr/6
    iex:3: (file)

However by examining certain codebase, I discovered there is another, more cryptic, but more character-efficient way to do so: %_{}.

iex(3)> %_{} = %Order{id: 10}
%Order{id: 10}
iex(4)> %_{} = %{id: 10}
** (MatchError) no match of right hand side value:

    %{id: 10}

    (stdlib 7.1) erl_eval.erl:672: :erl_eval.expr/6
    iex:4: (file)

A small thing, but showing how Elixir can still surprise me.

Update (2026-01-27)

As Tyler Young pointed out, it’s also possible to destructure a module name this way:

iex(4)> %modname{} = %Order{id: 10}
%Order{id: 10}
iex(5)> modname
Order