我有一个包含任何功能的行为。
defmodule MyBehaviour do
@callback do_run( ? ) :: ? #the ? means I don't know what goes here
defmacro __using__(_) do
quote location: :keep do
@behaviour MyBehaviour
def run, do: MyBehaviour.run(__MODULE__, [])
def run(do_run_args), do: MyBehaviour.run(__MODULE__, do_run_args)
end
end
def run(module, do_run_args) do
do_run_fn = fn ->
apply(module, :do_run, do_run_args)
end
# execute do_run in a transaction, with some other goodies
end
end
defmodule Implementation do
use MyBehaviour
def do_run(arg1), do: :ok
end
Implemenation.run([:arg1])
这个想法是,通过实现MyBehaviour,模块Implementation将具有run([:arg1])将调用的函数do_run(:arg1)。
如何@callback为具有可变数量参数的函数编写规范?
我认为这@callback do_run(...) :: any()会起作用,但 Dialyzer 给了我一个错误Undefined callback function do_run/1,所以我认为...这意味着任何参数,但不是零参数。
实际上,我只有两种情况:零和一个 arg。我想过像这样重载规范:
@callback do_run() :: any()
@callback do_run(any()) :: any()
但这需要两个do_run函数,因为在 Erlang 世界中,同名和不同的数量是两个独立的函数。
如果我做到了,它们@optional_callback都有可能不会被实施。
@type允许像这样指定任何数量的函数,(... -> any())所以我想应该可以对@callback.
是否可以在不重新实现行为的情况下正确指定这一点?