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

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
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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 30048 115
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

We're in Beta

About us Mission Statement