# Multiple callbacks for same function, different arguments

**URL:** <https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601>\
**Category:** Questions\
**Tags:** behaviours\
**Created:** [August 4, 2018, 5:39am UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601 "2018-08-04T05:39:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![desmond](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/desmond/32/1468_2.png) [@desmond](https://forum.elixirforum.com/u/desmond)\
**Post date:** [August 4, 2018, 5:39am UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/1 "2018-08-04T05:39:38Z")

</div>

My application has many serializers for turning structs into something suitable for clients. These serializers have many `serialize/1` functions defined to handle the nil case, list case, and finally the struct itself:

```elixir
defmodule MyApp.ItemSerializer do
  def serialize(nil), do: nil
  def serialize(items) when is_list(items) do
    Enum.map(items, serialize/1)
  end

  def serialize(item) do
    %{
          # ...
        }
  end
end

```

I’d like to have a `@behaviour MyApp.Serializer` directive to guarantee that individual implementations follow the contract of implementing all the `serialize/1` functions. The problem is, it looks like callbacks only respect one function implementation, and creating many callbacks with the same function name but different typespecs does not help.

Is this the expected --ahem-- behavior of callbacks?

---

<div class="post-metadata">

**Author:** ![tty](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/0ea827/32.png) [@tty](https://forum.elixirforum.com/u/tty)\
**Post date:** [August 4, 2018, 6:38am UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/2 "2018-08-04T06:38:15Z")

</div>

This is linked to how functions are differentiated in the BEAM. Functions are differentiated by the module they reside in, their name and arity (number of arguments the function accepts). As such **serialize/1** is the same function and the various function clauses a contraction of an **if-else**.

If having these 3 cases are import the define the 3 different functions in the behaviour individually e.g. **serialize\_nil/1** , **serialize\_lists/1** , **serialize\_item/1**. The behaviour can auto-generate a **serialize/1** which calls the relevant callbacks.

If you have many different structs perhaps look into using Protocols.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [August 4, 2018, 8:17am UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/3 "2018-08-04T08:17:39Z")

</div>

Why don’t you make it a `protocoll`? Then you could implement the list-case once in a `defimpl X, for: List`, the `nil` in `defimpl X, for: Atom`, while having discrete `defimpl X, for: Y` for your different structs.

---

<div class="post-metadata">

**Author:** ![desmond](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/desmond/32/1468_2.png) [@desmond](https://forum.elixirforum.com/u/desmond)\
**Post date:** [August 4, 2018, 6:39pm UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/4 "2018-08-04T18:39:46Z")

</div>

The problem with using a Protocol is I want to be able to support different versions of the serializer for the same object, so `GameSerializer.V1.serialize/1` and `GameSerializer.V2.serialize/1`, with the caller selecting which one to use. So I can’t just call a generic `Serializer.serialize/1` without knowing what I’m passing in (list, nil, actual object) which I believe rules out the use of a single protocol. Unless I’m missing something?

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [August 6, 2018, 6:09pm UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/5 "2018-08-06T18:09:04Z")

</div>

How do you determine the version?

---

<div class="post-metadata">

**Author:** ![desmond](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/desmond/32/1468_2.png) [@desmond](https://forum.elixirforum.com/u/desmond)\
**Post date:** [August 6, 2018, 7:33pm UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/6 "2018-08-06T19:33:15Z")

</div>

The controller itself calls either the `V1` or `V2` serializer, since the controller/route is namespaced with versions as well. So basically hardcoded.

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [August 6, 2018, 8:46pm UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/7 "2018-08-06T20:46:56Z")

</div>

> [@desmond](#):
>
> The controller itself calls either the `V1` or `V2` serializer, since the controller/route is namespaced with versions as well. So basically hardcoded.

Why not encode that into the dispatch argument then? 🙂

Or use a protocol per version that can delegate to the lower version for the parts that are the same.

Or use my [ProtocolEx](https://hex.pm/packages/protocol_ex) to dispatch on both the version and value.

There are even more things you can do. 🙂

---

<div class="post-metadata">

**Author:** ![michalmuskala](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/32/20288_2.png) [@michalmuskala](https://forum.elixirforum.com/u/michalmuskala)\
**Post date:** [August 6, 2018, 9:11pm UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/8 "2018-08-06T21:11:51Z")

</div>

Here’s an idea - use a protocol with two callbacks - `serialize_v1` and `serialize_v2`. For implementations where those are close enough or even the same, it’s pretty easy to delegate to a common implementation.

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [August 6, 2018, 9:31pm UTC](https://forum.elixirforum.com/t/multiple-callbacks-for-same-function-different-arguments/15601/9 "2018-08-06T21:31:13Z")

</div>

> [@michalmuskala](#):
>
> Here’s an idea - use a protocol with two callbacks - `serialize_v1` and `serialize_v2` . For implementations where those are close enough or even the same, it’s pretty easy to delegate to a common implementation.

> Or use a protocol per version that can delegate to the lower version for the parts that are the same.

Yep, precisely my second suggestion. ^.^
