Module.create
create
, go back to Module module for more information.
Specs
Creates a module with the given name and defined by the given quoted expressions.
The line where the module is defined and its file must be passed as options.
It returns a tuple of shape {:module, module, binary, term}
where module
is the module name, binary
is the module
bytecode and term
is the result of the last expression in
quoted
.
Similar to Kernel.defmodule/2
, the binary will only be
written to disk as a .beam
file if Module.create/3
is
invoked in a file that is currently being compiled.
Examples
contents =
quote do
def world, do: true
end
Module.create(Hello, contents, Macro.Env.location(__ENV__))
Hello.world()
#=> true
Differences from defmodule
Module.create/3
works similarly to Kernel.defmodule/2
and return the same results. While one could also use
defmodule
to define modules dynamically, this function
is preferred when the module body is given by a quoted
expression.
Another important distinction is that Module.create/3
allows you to control the environment variables used
when defining the module, while Kernel.defmodule/2
automatically uses the environment it is invoked at.