Last active
March 5, 2016 21:03
-
-
Save maximvl/527aa404c889d2f30155 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 T.Behaviour do | |
defmacro __using__(_) do | |
quote do | |
@behaviour T.Behaviour | |
import T.Macros, only: [deftest: 2] | |
deftest :a do | |
IO.puts z | |
end | |
end | |
end | |
end | |
defmodule T.Macros do | |
defmacro deftest(name, do: body) do | |
IO.puts inspect body | |
quote do | |
def unquote(name)(var!(z)) do | |
unquote(body) | |
end | |
end | |
end | |
end | |
defmodule T.Impl do | |
use T.Behaviour | |
end | |
defmodule T.OK do | |
import T.Macros, only: [deftest: 2] | |
deftest :a do | |
IO.puts z | |
end | |
end | |
## Trying to compile this: | |
# > mix | |
# lib/example.ex:25: warning: behaviour T.Behaviour undefined | |
# lib/example.ex:26: warning: variable z is unused | |
# == Compilation error on file lib/example.ex == | |
# ** (CompileError) lib/example.ex:26: function z/0 undefined | |
# (stdlib) lists.erl:1337: :lists.foreach/2 | |
# (stdlib) erl_eval.erl:669: :erl_eval.do_apply/6 | |
## If the T.Impl module is commented: | |
# > mix | |
# Compiled lib/example.ex | |
# Generated app | |
# > iex -S mix | |
# Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] | |
# Interactive Elixir (1.1.1) - press Ctrl+C to exit (type h() ENTER for help) | |
# iex(1)> T.OK.a 5 | |
# 5 | |
# :ok | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment