[erlang-questions] What is the fastest way to check if a function is defined in a module?

Metin Akat akat.metin@REDACTED
Sat Mar 3 20:42:41 CET 2018


I'm looking into this code:
https://github.com/erlang/otp/blob/master/lib/stdlib/src/gen_server.erl#L631

Seems like this try/catch clause is implemented with the idea that most of
the time it will succeed, as the callback module will have implemented the
corresponding callback function, so we will not have to go into the
exception clause where it needs to call erlang:function_exported/2 one more
time (presumably as an additional safeguard) which is more expensive than
an optimistic try clause.

I am writing something very similar, where such a check (is a function
exported) will be performed by thousands/millions of processes on every
message, but in my case... it's quite probable that most of the time this
WILL raise the exception. In my case the function will be purely optional -
if defined, it will return a value. If not, a default value will be used.

So my question is, how bad is this? I will test, but I'm still interested
if someone has any ideas (maybe do something completely different).

Other possibilities are:

- Actually make the callback compulsory. I don't want to do that, as the
problem I'm solving with this project is "See, you don't have to implement
this boilerplate every time any more"

-  Put the option in application env where the user can configure it on
startup.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180303/7fe1995c/attachment.htm>


More information about the erlang-questions mailing list