mudasobwa

mudasobwa

Creator of Cure

How can I access typespecs of compiled but not yet stored to BEAM file module?

I am struggling to figure out how can I get to typespecs in the sibling module during the compilation phase.

The minimal viable example would be:

defmodule Typespecs.Types do
  @type my_type :: atom()
end

defmodule Typespecs do
  alias Typespecs.Types

  # Try to load typespecs from the sibling module.
  IO.inspect(Code.Typespec.fetch_types(Types), label: "Types #1")
  Types = Code.ensure_compiled!(Types)
  IO.inspect(Code.Typespec.fetch_types(Types), label: "Types #2")
end

it evidently produces :error in both cases because Code.Typespec.fetch_types/1 requires a BEAM file and at the moment it’s not yet flushed to disk.

Kernel.Typespec.translate_typespecs_for_module/2 is not quite helpful because :ets tables are available only while the module (Types in this case) is open and it’s already closed at the moment I get to it.

I have tried to implement my own compile tracer, but there is no proper event to grab the content of :ets tables and store it somewhere at fingers, plus this solution seems to be too hacky.

I am positive I am missing something obvious. Any insights on how can I get to typespecs of already compiled but not yet stored to the disk module?

Most Liked

axelson

axelson

Scenic Core Team

I feel compelled to point out that Code.Typespec is @moduledoc false (source_link) so it is private and you shouldn’t be calling it in your code as it’s behavior could change in any Elixir patch release.

Unfortunately I’m not aware of any public function to fetch typespecs (although one may exist), if there was a public function it would be nice to use it in ElixirSense (which powers ElixirLS).

Where Next?

Popular in Questions Top

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
Tee
can someone please explain to me how Enum.reduce works with maps
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
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
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

Other popular topics Top

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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
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 31194 112
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
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
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement