Is there a way to return function definitions in Elixir

Give me the module:

defmodule Foo do
  def bar(baz) do
    IO.puts baz
  end
end

Is there a way I can return:

def bar(baz) do
  IO.puts baz
end

I developed that I can download the full module definition using

Foo.__info__(:compile) |> List.last |> elem(1) |> File.read |> elem(1)

But ideally, I would like to do something like

Foo.bar/1.__definition__
#=> def bar(baz) do\n  IO.puts baz\nend\d
+4
source share
1 answer

Elixir is a compiled language. At runtime, the source code no longer exists; it was linked to the BEAM byte code. At your code level, there is no initial representation of a function at run time to check or retrieve code.

+1
source

Source: https://habr.com/ru/post/1627319/


All Articles