$ iex -S mix
iex(1)> B.new_hoge
:fuga
Created
January 31, 2022 15:13
-
-
Save tamanugi/5deb02d336553cb8d8a6d17664c90e88 to your computer and use it in GitHub Desktop.
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 A do | |
@ignore_attributes [:macrocallback, :before_compile, :opaque, :spec, :behaviour, :on_definition, | |
:type, :dialyzer, :external_resource, :after_compile, :callback, | |
:derive, :compile, :optional_callbacks, :moduledoc, :typep] | |
defmacro __using__(_opts \\ []) do | |
quote do | |
@before_compile A | |
end | |
end | |
defmacro __before_compile__(env) do | |
Module.attributes_in(env.module) | |
|> Enum.reject(&(&1 in @ignore_attributes)) | |
|> Enum.map(fn attr -> | |
quote do | |
def unquote(:"new_#{attr}")() do | |
unquote(Module.get_attribute(env.module, attr)) | |
end | |
end | |
end) | |
end | |
end |
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 B do | |
use A | |
@hoge :fuga | |
@moduledoc """ | |
Documentation for `Metatest`. | |
""" | |
@doc """ | |
Hello world. | |
## Examples | |
iex> Metatest.hello() | |
:world | |
""" | |
def hello do | |
:world | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment