Could somebody provide a simple example for defdelegate
. I couldn't find any, making it hard to understand.
defmodule Dummy do
def hello, do: "hello from dummy"
end
I get undefined function world/0
for the below:
defmodule Other do
defdelegate hello, to: Dummy, as: world
end
I would like to delegate Other.world
to Dummy.hello
Two things:
You got the name and as:
wrong. as:
should contain the name of the function in the target module and the first argument should be the name to define in the current module.
The argument to as
needs to be an atom.
Final working code:
defmodule Dummy do
def hello, do: "hello from dummy"
end
defmodule Other do
defdelegate world, to: Dummy, as: :hello
end
IO.puts Other.world
Output:
hello from dummy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With