Skip to content

Instantly share code, notes, and snippets.

@kopos
Created April 24, 2018 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kopos/2e3722c9af604a4de24f80630d916415 to your computer and use it in GitHub Desktop.
Save kopos/2e3722c9af604a4de24f80630d916415 to your computer and use it in GitHub Desktop.
Small script to understand usage of __using__ / $ elixirc hello_using.exs
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