Skip to content

Instantly share code, notes, and snippets.

@mrkaspa
Last active January 8, 2016 06:54
Show Gist options
  • Save mrkaspa/50e3ce762696df8bdc7e to your computer and use it in GitHub Desktop.
Save mrkaspa/50e3ce762696df8bdc7e to your computer and use it in GitHub Desktop.
If else built in elixir using macros
defmodule My do
defmacro ifx(conds, [do: code1, else: code2]) do
quote do
case unquote conds do
true -> unquote code1
_ -> unquote code2
end
end
end
end
defmodule Test do
require My
def demo do
a = 10
My.ifx a == 10 do
IO.puts "yes"
else
IO.puts "no"
end
end
end
Test.demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment