[erlang-questions] Recommendations on optional callbacks
Loïc Hoguin
essen@REDACTED
Tue Jul 22 10:41:15 CEST 2014
On 07/22/2014 05:05 AM, Garrett Smith wrote:
> On Mon, Jul 21, 2014 at 2:07 PM, Jay Nelson <jay@REDACTED> wrote:
>> We have created a behaviour with -callbacks, but would like to simplify
>> the implementation by providing default implementations of some of the
>> callbacks. Using erlang:function_exported/3 to test for a user-supplied
>> callback and instead calling a default implementation may work, but the
>> compiler will complain for the user-defined module if no callback is
>> supplied. What is the recommended best practice for this situation?
>> The best I can come up with is to document the default implementation
>> and leave it up to the user-defined module to call that function or
>> provide its own so that the callback is always present.
Don't use -behaviour.
That functionality gives you two things: a check that the functions are
exported (pointless for optional callbacks), and a *default* -spec that
is generally much larger than what the function actually does. This does
*not* saves you having to write the -spec for them if you care about
typespecs.
> For performance sake, the higher order module notes the list of
> exports on start_link (or other entry point) and stores them in state
> rather than test on each call. This is sort of obvious though.
Terrible idea. This screws code reloading and isn't actually optimizing
anything in a real world scenario.
--
Loïc Hoguin
http://ninenines.eu
More information about the erlang-questions
mailing list