jeremy.owensboggs

jeremy.owensboggs

Defoverridable disclaimer to use it with care - but why?

While perusing the defoverridable documentation, one of my teammates pointed out the disclaimer:

Use defoverridable with care. If you need to define multiple modules with the same
behaviour, it may be best to move the default implementation to the caller, and check if a callback exists via Code.ensure_loaded?/1 and function_exported?/3.

The disclaimer doesn’t explain why this is. What is the reason for this disclaimer? Why is it be preferable to query the module for an implementation, rather than use a defoverridable? Is it a performance thing? Readability thing? Other?

Most often I see defoverridable used with a using macro - does that come into play with this disclaimer?

Most Liked

josevalim

josevalim

Creator of Elixir

The issue with defoverridable are two:

  1. It leads to broad interfaces because you can define default functions that are “inherited” by everyone. While this is also possible with callbacks, the fact a callback is optional means you cannot rely on it as part of your API. This is part of an overall principle were you generally want to keep your interfaces narrow.

  2. Overridable functions come from meta-programming, which is harder to debug, and an overriden implementation can call their previous definition using super, which is not something you can turn on or off and creates additional coupling, regardless if it is intentional or not. super can also cascade, which leads to more issues during trouble-shooting.

There are situations were defoverridable is preferred and I think @slouchpie gave a good example, where you don’t want to invoke Code.ensure_loaded? on every time. But for process-based behaviours or behaviours with clear entry-points, I’d recommend optional callbacks. I will improve the documentation.

As I said at the time, saying there are several instances does not move the needle, because the Elixir team doesn’t know where they are. My biggest concern is that, without quantifying them, we may be simply repeating information which is no longer true. Especially because, if there are examples such as this discussion, then I am certain they will be addressed.

EDIT: Improve docs for defoverridable and behaviours · elixir-lang/elixir@38f460e · GitHub

mudasobwa

mudasobwa

Creator of Cure

Normally, the implementations are called through the dependency injection and they don’t need any use clause. Like this

defmodule TestBeh do
  @callback ok :: :ok
  @callback ko :: :ko

  @optional_callbacks ko: 0

  @default_impl Application.compile_env(:foo, :test_beh, TestImpl)

  # default implementation
  def ko, do: :ko

  def ok(impl \\ @default_impl), do: impl.ok()
  def ko(impl \\ @default_impl) do
    impl = if function_exported(impl, :ko, 0), do: impl, else: __MODULE__
    impl.ko()
  end 
end
slouchpie

slouchpie

Imagine we want to use a behaviour like this:

defmodule NameBehaviour do
  @callback name :: String.t()
  @optional_callbacks [name: 0]
end

APPROACH 1
With defoverridable it would be:

defmodule Greeter do
  defmacro __using__(_opts) do
    quote do
      @behaviour NameBehaviour

      defdelegate name, to: unquote(__MODULE__)
      defoverridable name: 0
    end
  end

  def name do
    "stranger"
  end

  def greet(module) do
    "Hello " <> module.name()
  end
end

APPROACH 2
Without defoverridable (as docs suggest) it would be:

defmodule Greeter do
  defmacro __using__(_opts) do
    quote do
      @behaviour NameBehaviour
    end
  end

  def greet(module) do
    if Code.ensure_loaded?(module) and function_exported?(module, :name, 0) do
      "Hello " <> module.name()
    else
      "Hello stranger"
    end
  end
end

Personally, I think “Approach 1” looks much better.

Surely it is not good to have this Code.ensure_loaded? and function_exported? runtime evaluation every time I call this function?

Update: function_exported? is inlined by the compiler but I don’t know if Code.ensure_loaded? is.

Last Post!

mudasobwa

mudasobwa

Creator of Cure

Exactly.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement