有人可以提供一个简单的例子defdelegate.我找不到任何东西,让人难以理解.
defmodule Dummy do
def hello, do: "hello from dummy"
end
Run Code Online (Sandbox Code Playgroud)
我得到undefined function world/0以下内容:
defmodule Other do
defdelegate hello, to: Dummy, as: world
end
Run Code Online (Sandbox Code Playgroud)
我想委托Other.world给Dummy.hello
Dog*_*ert 21
两件事情:
你得到了名字和as:错误.as:应包含目标模块中函数的名称,第一个参数应该是当前模块中要定义的名称.
as需要成为原子的论据.
最终工作代码:
defmodule Dummy do
def hello, do: "hello from dummy"
end
defmodule Other do
defdelegate world, to: Dummy, as: :hello
end
IO.puts Other.world
Run Code Online (Sandbox Code Playgroud)
输出:
hello from dummy
Run Code Online (Sandbox Code Playgroud)