[erlang-questions] Function name macro (the counterpart of ?MODULE)

Paul Mineiro paul-trapexit@REDACTED
Mon Dec 21 21:51:59 CET 2009


On Mon, 21 Dec 2009, Attila Rajmund Nohl wrote:

> 2009/12/21, Ciprian Dorin, Craciun <ciprian.craciun@REDACTED>:
> >     Hy all!
> >
> >     Maybe this question has been asked a thousand times, but please
> > forgive and bare with me just one more time... :)
> >
> >     So as predefined macros we have ?MODULE, ?FILE, and ?LINE... But
> > there is no ?FUNCTION (that should give me the current function name).
> >
> >     Now my question is: why isn't there any such macro? (I would guess
> > that having such a macro is not so complicated...) And what can I do
> > to obtain a similar functionality?
>
> I haven't tested, but you can try something like this:
>
> catch throw x,
> [{_, Fun, _} | _] = erlang:get_stacktrace().

for a solution without run-time overhead, you could write a parse
transform which looked for a function call like _FUNCTION () and
transformed it to a constant atom.

ulf's ct_expand is easy to modify to do these kinds of things.

-- p


More information about the erlang-questions mailing list