Skip to content

Instantly share code, notes, and snippets.

@imranismail
Last active June 18, 2017 19:48
Show Gist options
  • Save imranismail/464fc1622b722e5ab11674db091ac083 to your computer and use it in GitHub Desktop.
Save imranismail/464fc1622b722e5ab11674db091ac083 to your computer and use it in GitHub Desktop.
Trying to figure out function head bindings for Elixir Macro
defmodule Aggregate do
defmacro replay(struct, aggregate, do: block) do
quote bind_quoted: [struct: struct] do
@events [struct|@events]
def __replay__(struct, unquote(aggregate)), do: unquote(block)
end
end
end
defmodule User do
defstruct [:activated?]
replay %UserCreated{} = event, aggregate do
%{aggregate | activated?: event.activated?}
end
end
def User do
def __replay__(%UserCreated{} = event, aggregate) do
%{aggregate | activated?: event.activated?}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment