3 ms·
I'm a contrarian here. I really wished we had inheritance in Elixir for GenServers - because those are objects, interacting out of messages and not just glorifi
by kimi 3y ago
I'm a contrarian here. I really wished we had inheritance in Elixir for GenServers - because those are objects, interacting out of messages and not just glorified structs with associated functions.
So I wish I could have a generic template for a project's GenServer (for basic common registration, memory configuration, logging), that is specialized for a specific task, of which there are three flavours.
Speaking of footguns, Elixir patches this problem with macro code generation, but it is not so good. Any my GenServers are full of trivial repetitions.
- POiNTx 3y agoBehaviours might be what you're looking for, they're the closest thing to inheritance in Elixir. And you're absolutely right about genservers being objects :)
- kimi 3y agoThey are, but it's not as natural as A specializes B that specializes C.
- toast0 3y agoIt's maybe not quite as formalized inheritance, but (at least in Erlang) you can make behavior B that is a behavior A, and in module C (which implements behavior B), do B:start(C, ...) B;start/? Most likely does A:start(B, ...) and when the callbacks come in, B may call functions from C or not depending on things (such as if there is an optional callback defined). If your boilerplate turns out to not really be that samey though, it's easier IMHO to have a template and customize it where applied.