Skip to content

Instantly share code, notes, and snippets.

@sionide21
Created November 23, 2023 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sionide21/cdeb122109ea3cfa317880bd28890874 to your computer and use it in GitHub Desktop.
Save sionide21/cdeb122109ea3cfa317880bd28890874 to your computer and use it in GitHub Desktop.
defmodule Debug do
defmacro debug_case(ast) do
ast
|> Macro.postwalk(fn
{:->, ctx, [[clause], expr]} ->
debug = quote do
IO.puts("Hit clause: #{unquote(Macro.to_string(clause))}")
end
{:->, ctx, [[clause], {:__block__, [], [debug, expr]}]}
ast -> ast
end)
end
end
defmodule MyCode do
require Debug
def some_function(input) do
case {input, 0} do
{:cat, _} -> "Meow"
{:dog, 0} -> "Woof"
_ -> "???"
end
|> Debug.debug_case
end
end
iex(1)> MyCode.some_function(:cat)
Hit clause: {:cat, _}
"Meow"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment