Created
February 16, 2016 15:53
-
-
Save arjan/16ed99f656eef9b8fd58 to your computer and use it in GitHub Desktop.
GenServer macros
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 GenServer.Interface do | |
defmacro defcall(function, arity) do | |
args = for n <- :lists.seq(1,arity), do: {"arg" <> Integer.to_string(n) |> String.to_atom, [], Elixir} | |
quote do | |
def unquote(function)(server, unquote_splicing(args)) do | |
GenServer.call(server, {unquote(function), unquote_splicing(args)}) | |
end | |
end | |
end | |
defmacro defcast(function, arity) do | |
args = for n <- :lists.seq(1,arity), do: {"arg" <> Integer.to_string(n) |> String.to_atom, [], Elixir} | |
quote do | |
def unquote(function)(server, unquote_splicing(args)) do | |
GenServer.cast(server, {unquote(function), unquote_splicing(args)}) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
generates: