emilsoman

emilsoman

I recently learned that it’s possible to delegate common implementations of protocols using defdelegate in the implementations, but I’m curious to know if this is possible using a macro that provides overridable functions.

The following code works, but shows a warning (see comment below) :

defprotocol MyProtocol do
  def foo(data)
  def bar(data)
end

defmodule MyProtocol.Defaults do
  defmacro __before_compile__(_env) do
    quote do
      def bar(data) do
        IO.puts "Default implementation of bar, data: #{data}"
      end
      defoverridable [bar: 1]
    end
  end
end

defimpl MyProtocol, for: Integer do
  @before_compile MyProtocol.Defaults

  def foo(data) do
    IO.puts "int implementation of foo, data: #{data}"
  end

  # Is it possible to override `bar` from before_compile without this warning:
  #
  # warning: this clause cannot match because a previous clause at line 28 always matches
  # my_protocol.exs:17
  def bar(data) do
    IO.puts "int implementation of bar, data: #{data}"
  end
end

Do I get this warning because before_compile’s return value is injected at the end of the module? Is there any way to get around this?

Showing Posts 1 to 5

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I’m not sure I agree with this approach to begin with, but if you were to go this way I think it makes more sense to do a __using__ instead of a before compile. The the function is injected at the top, and you override it in your defimpl block. As it is it’s always at the bottom, which is likely what prevents you from overriding it.

emilsoman

emilsoman OP

@benwilson512

What’s the idiomatic way to do this?

Yes, this works. So I guess the problem with before_compile here is that it’s appended to the end of the module body. It makes sense, then. Thanks!

josevalim

josevalim

Creator of Elixir

You can explicitly delegate to the function you want to:

defimpl MyProtocol, for: Integer do
  def foo(data) do
    MyProtocol.Default.foo(data)
  end

  ...
end

You can even use defdelegate to make it more compact:

defimpl MyProtocol, for: Integer do
  defdelegate foo(data), to: MyProtocol.Default
  ...
end

The question you want to ask yourself is: are you really expecting to write the code above so many times to justify the use of indirection and meta-programming? The answer is likely no.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

My thoughts precisely.

Ajwah

Ajwah

Can a case be made to have default implementations be delegated to Any when its implementation is missing?

defprotocol Duration do
   @fallback_to_any true

   def seconds(a)
   def compare(a, b)
end
defimpl Duration for: Any do
    def seconds(_), do: raise "Not yet implemented"
    def compare(_, _), do: raise "Not yet implemented"
end
defimpl Duration for: NaiveDateTime do
   def compare(a, b) do
       NaiveDateTime.compare(a, b)
   end

   # we could implement `seconds` here in the meaning of epoch; but we could also skip it's implementation if we decide there is no semantic need for this. 
end

The fact we have @fallback_to_any true means that it is not unreasonable to expect that missing implementations are defaulted to Any; so still explicit. Currently a warning is rendered for missing implementations; so the above fallback would get rid of the warning while clearly communicating that its implementation has been omitted deliberately(which could have several reasons, among which is because the domain does not require its implementation.)

Cons to the above suggestion:

  • Backwards compatibility of the language(This thread is like 6 years old, lol)
  • Protocol should always be fully implementable. If stuff remains omitted then this means the protocol is too broad and it should be made smaller. Counter argument: Naming things is hard and sometimes it is not worthwhile to split up existing protocols in more granular ones.
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews