Created
April 28, 2016 11:42
-
-
Save brweber2/fcfbf34526884a88f2e9e6b24178b147 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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