No. Module-Attributes can’t be used in another module. At least not without certain coding overhead.
The following is not tested, but should at least push you into the direction:
defmodule A do
Module.register_attribute __MODULE__, :vote_type, persist: false
@vote_type "7"
end
defmodule B do
require A # make sure `A` is compiled before `B`
@vote_type Module.get_attribute(A, :vote_type)
def create(conn, %{"type": @vote_type, ...}, do: ...
end
This way, you should be able to do them centralised. Another way would be to use use:
defmodule A do
defmacro __using__([]) do
quote do
@vote_type "7"
end
end
end
defmodule B do
use A
def create(conn, %{"type": @vote_type, ...}, do: ...
end
That will then be inlined and will work in function heads and across modules, a defmacro is called at compile-time and returns an AST, in this case "7" is a valid AST so it will work as expected.
There are enum-style libraries that make this more obvious too.