# Polymorphism in Elixir

**URL:** <https://forum.elixirforum.com/t/polymorphism-in-elixir/72196>\
**Category:** Blog Posts\
**Tags:** via-devtalk, blog-post, blogs-by-jkwa\
**Created:** [August 22, 2025, 4:18am UTC](https://forum.elixirforum.com/t/polymorphism-in-elixir/72196 "2025-08-22T04:18:19Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![adamu](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/adamu/32/31482_2.png) [@adamu](https://forum.elixirforum.com/u/adamu)\
**Post date:** [August 22, 2025, 6:15am UTC](https://forum.elixirforum.com/t/polymorphism-in-elixir/72196/2 "2025-08-22T06:15:35Z")

</div>

> I didn’t have room for this in my book

> This topic is explored in more depth in my book

These two statements contradict each other, perhaps the footer on the blog is generic and doesn’t apply in this case?

I think [behaviours](https://forum.elixirforum.com/t/help-understanding-protocols-and-behaviours/57229/4) also deserve a mention along with protocols. My experience is that it’s quite rare to write a protocol, compared to a behaviour.

For example, `Eq` could also be implemented using a behaviour:

```elixir
defmodule Eq do
  @callback eq?(a :: struct, b :: map) :: boolean

  def eq?(%type{} = a, b) do
    type.eq?(a, b)
  end
end

defmodule Cat do
  defstruct [:name, :microchip]

  @behaviour Eq

  def eq?(%Cat{microchip: a}, %{microchip: b}), do: a == b
end

```

```elixir
iex(1)> Eq.eq?(%Cat{microchip: "foo"}, %{microchip: "foo"})
true
iex(2)> Eq.eq?(%Cat{microchip: "foo"}, %{microchip: "bar"})
false

```

---

_[View the full topic](https://forum.elixirforum.com/t/polymorphism-in-elixir/72196)._
