macros - Elixir: generating module with proper import -
i'm trying write macro generates module:
defmodule genmodules defmacro module(name, do: body) quote defmodule unquote(string.to_atom(name)) unquote(body) end end end end what i'm missing how inject 'import' statement refer module macro called from?
i.e., following code:
defmodule test import genmodules module "newmodule" def test_call hello_world end end def hello_world io.puts "hello" end end won't compile because hello_world function not visible generated newmodule.
so need generate
import test before body of module, somehow getting name of module macro called from. how do this?
thank you, boris
to module macro called can use special form __caller__. contains bunch of information, can extract calling module so:
__caller__.context_modules |> hd but more don't think want possible - cannot import module before finish defining it. example:
defmodule defmodule b import end end results in error:
** (compileerror) test.exs:3: module not loaded defined. happens because trying use module in same context defined. try defining module outside context requires it.
Comments
Post a Comment