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

rms.mrcs
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
9mm
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
freewebwithme
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
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
WestKeys
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 Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sergio
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
senggen
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
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