Do you use `defoverridable` (and `super`) in your code?

Hello,

What are you using defoverridable and super for?

I have searched for uses of these features, in large projects like Ecto, Ecto_SQL and Hex.pm, and only Ecto_SQL makes use of defoverridable, but only at one place. In addition, there is no trace of super being used in these projects.

Are these features not of much use in FP (Functional Programming), or at least, in FP as implemented in Elixir / Erlang?

What do you think?

Thanks

The only places I’ve personally seen them used is when macros define default function implementations that could be overridden. For example, GenServer defines default implementation of handle_info etc. (see https://github.com/elixir-lang/elixir/blob/v1.8.1/lib/elixir/lib/gen_server.ex#L802), but the using module can of course define its own version that would override it. I image super is used mainly in those same cases.

3 Likes

So, in your own code you have not used them, but you see they being used to provide default implementations for behaviour callbacks, like the one you linked.

I wonder if that could not be also solved using dependency injection of the callback implementation to use :thinking:

1 Like

Very rarely. IIRC the only situation where I have used defoverridable so far is Opencensus.Plug library where user can define function for creating span name if they need to. It could be solved by injecting mfa(), but I think this solution is a little bit clearer and simpler.

2 Likes