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

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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

Other popular topics Top

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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New

We're in Beta

About us Mission Statement