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
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
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
Other popular topics
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
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
- #forms
- #api
- #metaprogramming
- #hex
- #security









