feifan

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 :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?

Marked As Solved

feifan

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)

Also Liked

kip

kip

ex_cldr Core Team

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.

Where Next?

Popular in Questions Top

Brian
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
jononomo
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
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
shijith.k
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
aalberti333
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
albydarned
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 Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
greenz1
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
gshaw
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
sergio_101
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jason.o
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

We're in Beta

About us Mission Statement