feifan
Get typespecs for Code.compile_string() module
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:errorKernel.Typespec.beam_specs(UserDefinedGreeter)returnsnil
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?
Marked As Solved
feifan
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
Also Liked
Popular in Questions
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
I would like to know what is the best IDE for elixir development?
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









