[erlang-questions] Recommendations on optional callbacks

Fred Hebert mononcqc@REDACTED
Tue Jul 22 14:51:50 CEST 2014


Erlang had a feature for this before Elixir even existed, and it was
taken out for various reasons (see http://www.erlang.org/news/35)

For backwards compatibility reasons, a parse transform supporting it
(the -extends(...). attribute) and parametrized modules was released as
a parse transform library: https://github.com/erlang/pmod_transform

The way to do it in OTP right now is just not to put the optional call
into the list of -callback or -behaviour_info, and dynamically check if
it's available.

The thing then gets documented, as is done for `format_status` for all
worker behaviours in OTP:

- http://www.erlang.org/doc/man/gen_server.html#Module:format_status-2
- http://www.erlang.org/doc/man/gen_fsm.html#Module:format_status-2
- http://www.erlang.org/doc/man/gen_event.html#Module:format_status-2

This does have the downside of not being able to provide type
information throught the -callback attributes however.

Regards,
Fred.

On 07/22, Alex Shneyderman wrote:
> elixir has a very nice solution to this. not sure if elixir is an option on
> your project. If not, at least in theory, you could use parse transforms
> (which are a bit of a pain - then again elixir will get rid of a lot of
> these pains) to add the missing methods and redirect to default
> implementations.
> 
> 
> On Mon, Jul 21, 2014 at 5: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.
> >
> > jay
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >

> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list