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

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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

We're in Beta

About us Mission Statement