I am close to using a behaviour as a pure interface and I feel dirty...

Brett Hemes brhemes@REDACTED
Tue Aug 24 00:00:33 CEST 2021

... should I?

I have some servers that carry around references to other (let's call them child) servers.  Each of the referenced child servers are doing their own thing and are implemented as different modules but also share some common functions (albeit with different implementations).  At some point, I end up fetching a reference to a child server and want to make a call that is common to all of the child servers.  As of right now I do not know what specific type (module) the child server is, only that they ALL offer some subset of functionality.

Assuming I am not approaching this entirely wrong (which could be the case) I have identified two leading options for my case:

1) use a behaviour (perhaps improperly) as a pure interface (i.e., with only callbacks and no "general" functionality).  For a child implemented in a `my_child_mod` module that implements behaviour `my_iface`, this could result in calls like `my_iface:a_common_fun(ChildServerRef)` after fetching the child server reference where I use the behaviour's module to access the child's functionality... OR

2) store the particular module of each child with the reference and use the stored module name at time of the call (and know/assume that all children have some shared functionality).  So using some tuple or similar `{my_child_mod, ChildServerRef}` (versus just the reference) could result in a call like `my_child_mod:a_common_fun(ChildServerRef)` after fetching the reference info (as a tuple).

I, perhaps naively, gravitate towards #1 above for various reasons (compile-time checks, only having to carry around refs, my C++ background, etc.), but the internet (and ferd in particular here (https://stackoverflow.com/questions/4119477/implementing-interfaces-in-erlang/4119615#4119615)) seems to imply that this is not the Erlang way (despite finding OTP cases like this: https://github.com/erlang/otp/blob/master/lib/ssh/src/ssh_sftpd_file_api.erl).   ... shame on me?

I am trying very hard to leave my C++ past behind and do things in the Erlang way, but need a little help in this particular case...

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210823/f325b0e7/attachment.htm>

More information about the erlang-questions mailing list