Skip to content

Instantly share code, notes, and snippets.

@brweber2
Created April 28, 2016 11:42
Show Gist options
  • Save brweber2/fcfbf34526884a88f2e9e6b24178b147 to your computer and use it in GitHub Desktop.
Save brweber2/fcfbf34526884a88f2e9e6b24178b147 to your computer and use it in GitHub Desktop.
defmodule Macros do
defmacro or_default(expr, default) do
quote do
try do
unquote(expr)
rescue
e ->
unquote(default)
end
end
end
end
defmodule MacrosTest do
use ExUnit.Case
test "macro expansion works properly" do
require Macros
assert 42 == Macros.or_default(100/0, 30 + 12)
assert 42 == Macros.or_default(30 + 12, flunk("this should not happen"))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment