feifan
1
Hello! Is it possible to get the typespecs for a module that is compiled at runtime via Code.complie_string()
? For example, if I run
compiled_module, _bytecode = Code.compile_string(
"""
defmodule UserDefinedGreeter do
@spec call(name :: String.t()) :: {:return, String.t()}
def call(name) do
{:return, "Hey " <> name}
end
end
"""
)
I end up with a UserDefinedGreeter
module and I can e.g. call UserDefinedGreeter.call("foo")
. However, I can’t seem to access that call
typespec.
-
Code.Typespec.fetch_types(UserDefinedGreeter)
returns :error
-
Kernel.Typespec.beam_specs(UserDefinedGreeter)
returns nil
Is there an option I’m overlooking, or a different way to compile Elixir code at runtime that will result in the typespecs being accessible?
1 Like
kip
2
Those functions retrieve information from the byte code on disk I believe, and therefore will fail for dynamically compiled modules.
I think you may find this post to be helpful. At least to understand the issue better.
4 Likes
feifan
3
Ah good to know — it looks like there might be a possible workaround in the post you linked. I’ll follow up there, thanks!
feifan
4
From that post you linked:
It turns out I can pass the bytecode to Code.Typespec.fetch_specs
directly. So from the original example, this should work:
_compiled_module, bytecode = Code.compile_string(
"""
defmodule UserDefinedGreeter do
@spec call(name :: String.t()) :: {:return, String.t()}
def call(name) do
{:return, "Hey " <> name}
end
end
"""
)
Code.Typespec.fetch_specs(bytecode)
1 Like