Hi, I have Elixir macro that I want to use it as @behaviour
in my project. but there is a problem I can not be able to use a @type
as a parameters of a macro.
My macro
defmodule MishkaPub.ActivityStream.Validator do
defmacro __using__(opts) do
quote(bind_quoted: [opts: opts]) do
type = Keyword.get(opts, :type)
module = Keyword.get(opts, :module)
@type t :: unquote(type)
@type action() :: :build | :validate
@callback build(t()) :: {:ok, action(), t()} | {:error, action(), any()}
@callback build(t(), list(String.t())) ::
{:ok, action(), t()} | {:error, action(), any()}
@callback validate(t()) :: {:ok, action(), t()} | {:error, action(), any()}
@callback validate(t(), list(String.t())) ::
{:ok, action(), t()} | {:error, action(), any()}
@behaviour unquote(module)
end
end
end
And the Elixir file I want to use it
defmodule MishkaPub.ActivityStream.Type.Object do
alias MishkaPub.ActivityStream.Validator
@type tt :: %__MODULE__{
id: String.t(),
type: String.t(),
name: String.t(),
replies: list(String.t())
}
defstruct [
:id,
:type,
..
]
use Validator, module: __MODULE__, type: tt()
def build(%__MODULE__{} = params) do
{:ok, :build, Map.merge(%__MODULE__{}, params)}
rescue
_e ->
{:ok, :build, :unexpected}
end
...
end
but I have this error
** (CompileError) lib/activity_stream/validator.ex:3: undefined function type/0 (there is no such import)
How can fix this error?
Another problem if this is fixed! I have 2 duplicated types. (tt()
, t()
) but I just want to have one type which should be t()
Thank you in advance