Hackling
Hackling

Reputation: 113

Is there a way I can return a functions definition in Elixir

Give I have a module:

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

Is there some way that I can return:

def bar(baz) do
  IO.puts baz
end

I've worked out that I can load the whole definition of module with:

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

But ideally, I'd love to be about to do something like

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

Upvotes: 6

Views: 841

Answers (1)

Scott Thompson
Scott Thompson

Reputation: 23701

Elixir is a compiled language. At runtime the source code is no longer there, it has been complied away to BEAM byte code. There is no source-level representation of the function at runtime for your code to inspect or retrieve.

Upvotes: 3

Related Questions