[erlang-questions] Module-Function-Arguments (MFA)

Sean Cribbs seancribbs@REDACTED
Fri Jun 26 19:48:16 CEST 2015


You should create a behavior module that defines the callbacks, which act
like -spec entries for the module that implements it.

%%----------------------
%% The "service" module.
-module(some_service).

%% Replace this with whatever argument and return types are needed.
-callback handle_tick() -> ok.

%% This module will probably also contain other things that you use to
drive the behavior of the client module.

%%-------------------------
%% The "client" module
-module(some_client).
-behaviour(some_service). %% This will force a check at compile-time that
the module implements the callbacks.
-export([handle_tick/0]).

%% Replace with the proper implementation and type spec.
-spec handle_tick() -> ok.
handle_tick() -> ok.

On Fri, Jun 26, 2015 at 10:51 AM, Avinash Dhumane <nistrigunya@REDACTED>
wrote:

> I need to implement a "service" functionality which allows the "client" to
> implement callbacks (from service into client's module) which it wishes to
> handle.
>
> I accept the module name as parameter from the client and mandate the
> function names (callbacks) and parameters thereof which the client may
> define in its module.
>
> Suppose one of the callback names is 'handle_tick', and the module name
> supplied by the client is held in variable 'Module' in my service
> implementation code.
>
> How do I verify whether client has implemented the function handle_tick()
> in its module? Or, do I just invoke it and handle the exception using
> try-catch?
>
> Thanks
> Avinash
>
> PS: I observe that the web sockets functionality in Yaws uses similar
> mechanism to invoke callbacks in client's module. But, digging into Yaws'
> source code is a little too overwhelming for me :-(
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150626/c28c3913/attachment.htm>


More information about the erlang-questions mailing list