Created
April 24, 2018 07:00
-
-
Save kopos/2e3722c9af604a4de24f80630d916415 to your computer and use it in GitHub Desktop.
Small script to understand usage of __using__ / $ elixirc hello_using.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyModule do | |
defmacro __using__(_) do | |
┆ quote do | |
┆ ┆ import MyModule | |
┆ ┆ def hi(s) do | |
┆ ┆ ┆ IO.puts "Hi #{s} from #{__MODULE__}" | |
┆ ┆ end | |
┆ end | |
end | |
def hello(s) do | |
┆ IO.puts "Hello #{s} from #{__MODULE__}" | |
end | |
end | |
defmodule ModA do | |
use MyModule | |
def my_hello(s), do: | |
┆ hello(s) | |
end | |
ModA.hi("there") | |
ModA.my_hello("world") | |
ModA.hello("world") # fail! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment