Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 664
Hackling Avatar asked Feb 04 '16 15:02

Hackling


1 Answers

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.

like image 54
Scott Thompson Avatar answered Nov 15 '22 10:11

Scott Thompson