Kernel.defdelegate
defdelegate
, go back to Kernel module for more information.
Defines a function that delegates to another module.
Functions defined with defdelegate/2
are public and can be invoked from
outside the module they're defined in, as if they were defined using def/2
.
Therefore, defdelegate/2
is about extending the current module's public API.
If what you want is to invoke a function defined in another module without
using its full module name, then use alias/2
to shorten the module name or use
import/2
to be able to invoke the function without the module name altogether.
Delegation only works with functions; delegating macros is not supported.
Check def/2
for rules on naming and default arguments.
Options
:to
- the module to dispatch to.:as
- the function to call on the target given in:to
. This parameter is optional and defaults to the name being delegated (funs
).
Examples
defmodule MyList do
defdelegate reverse(list), to: Enum
defdelegate other_reverse(list), to: Enum, as: :reverse
end
MyList.reverse([1, 2, 3])
#=> [3, 2, 1]
MyList.other_reverse([1, 2, 3])
#=> [3, 2, 1]