Gigitsu

Gigitsu

Get function signature at runtime

Summary

I’m trying to create a macro that takes in input a module name and automatically creates a defdelegate for each function defined in that module.

Example

defmodule MyApp.TargetModule1 do
  def foo(name, opts \\ []) do
    name <> to_string(opts)
  end

  def bar(name) do
    String.uppercase(name)
  end
end

defmodule MyApp.TargetModule2 do
  def baz(name) do
    "Ciao" <> name
  end
end


defmodule MyApp.DelegateModule do
  delegate_all MyApp.TargetModule1
  # the line above will generate the following:
  # defdelegate foo(name, opts \\ []), to: MyApp.TargetModule1
  # defdelegate bar(name), to: MyApp.TargetModule1

  delegate_all MyApp.TargetModule2
  # the line above will generate the following:
  # defdelegate baz(name), to: MyApp.TargetModule2
end

What I’ve done so far

I achieved this using Code.fetch_docs/1 and Code.Typespec.fetch_specs/1 but unfortunately these functions won’t work unless the .beam file isn’t already written on disk so when I modify any target module I get the :module_not_found error.

I know I can get the function list with MyApp.TargetModule1.__info__(:functions) but this will return a simple list of functions and arities without arguments names and possibly default arguments.

Questions

I already looked into ExUnit.DocTest but it also uses Code.fetch_docs/1 and I don’t know how does it work! Tests are compiled after the main lib files?

How can I get function signature (possibly also specs and documentations) from a module at runtime?

Most Liked

hauleth

hauleth

That doesn’t matter, as Erlang do not have notion of “default arguments” so what Elixir does to support them is to simply create multiple functions:

def foo(a, b \\ 1), do: …

Is the same as:

def foo(a), do: foo(a, 1)

def foo(a, b), do: …

Yes, exactly that.


AFAIK there is no way to fetch docs for in-memory module unless you store generated BEAM data in variable.

The only feasible approach is to use mod.__info__(:functions) (or mod.module_info(:functions) if you are willing to do filtering on your own).

Gigitsu

Gigitsu

With mod.__info__(:functions) and mod.module_info(:functions) I can’t know parameters name and which parameter ha a default.

My current work around, since I’m working on an umbrella project, is to put every delegate module in a separate app. In my specific case this workaround should work, but I was hoping in a more reliable solution.

Where Next?

Popular in Questions Top

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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
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
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
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
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
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

We're in Beta

About us Mission Statement