Skip to content

Instantly share code, notes, and snippets.

@vKxni
Last active July 7, 2022 17:09
Show Gist options
  • Save vKxni/2b4e45f3a993c337c422587696b935fa to your computer and use it in GitHub Desktop.
Save vKxni/2b4e45f3a993c337c422587696b935fa to your computer and use it in GitHub Desktop.
defmodule Main do
def print, do: "Hello from Main"
end
defmodule Other do
defdelegate hello, to: Main, as: :print
end
IO.puts(Other.hello)
@vKxni
Copy link
Author

vKxni commented Jul 7, 2022

Here, we are delegating Main.print to Other.hello.

Why IO.puts(Other.hello)?

Other is the new defmodule name, while .hello is our function (defdelegate) name, that refers to the main Main defmodule, but uses the function print (as an atom) as: :print

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment