Re: Specifying callbacks for behaviours extending gen_server
Leonard B <[email protected]>
| Newsgroups | gmane.comp.lang.erlang.general |
|---|---|
| Message-ID | <CAKj1m=+PiMVXf3odrFb4s2sRYzSsuM2ZegyPNpj5Cp-yrxk5Cw@mail.gmail.com> |
Hi Brett, This may help you out. http://davekuhlman.org/implement-a-behavior.html Fairly simple example and concise of a behavior wrapping a gen_server Kind regards, Leonard On Tue, Feb 23, 2021 at 11:18 AM Brett Hemes <[email protected]> wrote: > > Hah; Fernando after I replied I (think I) realized what you were saying. I now only specify my specific extensions in my behaviour using the `-callback` attribute and then include both my behaviour and gen_server in the user modules via `-behaivour`. I think this is a good solution to my dilemma. > > > > Thanks! > > Brett > > > > From: erlang-questions <[email protected]> On Behalf Of Brett Hemes > Sent: Tuesday, February 23, 2021 10:08 AM > To: [email protected] > Subject: [EXTERNAL] Re: Specifying callbacks for behaviours extending gen_server > > > > I was trying out the approach of the first answer here (https://stackoverflow.com/questions/6745183/erlang-extended-gen-server). In this approach, the extension behaviour get first crack at all of the gen_server messages and then forwards the user-module-specific stuff to the user module’s gen_server implementation. > > > > Basically I have written a bunch of (tested/working) TCP client/server interaction code as a gen_server and have a bunch of other modules that add specific functionality on top but require the same TCP handshaking. My idea was to wrap my working stuff as a behaviour but I can’t yet decide if I want to expose all of gen_server to my user modules or customize the interface. Right now I have opted for generality and am experimenting with exposing everything but we shall see... I am in that awkward place where I am working out my architecture while also trying to learn Erlang best practices; fun times. > > > > Brett > > > > From: Fernando Benavides <[email protected]> > Sent: Tuesday, February 23, 2021 9:26 AM > To: Brett Hemes <[email protected]> > Cc: [email protected] > Subject: [EXTERNAL] Re: Specifying callbacks for behaviours extending gen_server > > > > What if you only specify your own callbacks and let your implementors use... > > > > -behavior(your behavior). > > -behavior(gen_server). > > > > ? > > > > On Tue, 23 Feb 2021 at 16:15 Brett Hemes <[email protected]> wrote: > > I am designing a behaviour that extends gen_server and thus I want all of gen_server’s callback specification in addition to some additional ones on top as part of the extension. In the past I have favored using the `-callback` attribute as recommended in the documentation but then I saw someone recommend the following when extending gen_server > > > > > behaviour_info(Type) -> gen_server:behaviour_info(Type). > > > > I tried combining the two approaches but such is not allowed (which makes sense). I believe my options are to append my callbacks to gen_server’s list using `behaviour_info` or to copy gen_server’s callback specifications from the OTP source. Copy-pasting code just feels wrong... how bad is it to use `behaviour_info` over `-callback`? And am I taking the right approach? > > > > Thanks, > > Brett > > -- > > Sent from Gmail Mobile by Brujo Benavides