Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Last active June 6, 2024 22:37
Show Gist options
  • Save bcardarella/4d5655e0e7c90036754c25b94b2af4df to your computer and use it in GitHub Desktop.
Save bcardarella/4d5655e0e7c90036754c25b94b2af4df to your computer and use it in GitHub Desktop.

reproduce match?

Section

defmodule Foo do

  # stolen from:
  # https://github.com/elixir-lang/elixir/blob/v1.16.3/lib/elixir/lib/kernel.ex#L3401-L3412
  defmacro bar(pattern, expr) do
    success =
      quote do
        unquote(pattern) -> true
      end

    failure =
      quote generated: true do
        _ -> false
      end

    {:case, [], [expr, [do: success ++ failure]]}
  end
end
{:module, Foo, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:bar, 2}}
match?(_, nil)
true
Foo.bar(_, nil)
error: invalid use of _. _ can only be used inside patterns to ignore values and cannot be used in expressions. Make sure you are inside a pattern or change it accordingly
└─ /Users/bcardarella/Library/Application Support/livebook/autosaved/2024_06_06/22_13_a3vw/reproduce_match.livemd#cell:lb67qauyh2hfo2qh:1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment