# How does protocol dispatch work?

**URL:** <https://forum.elixirforum.com/t/how-does-protocol-dispatch-work/8114>\
**Category:** Discussions\
**Created:** [August 25, 2017, 4:00pm UTC](https://forum.elixirforum.com/t/how-does-protocol-dispatch-work/8114 "2017-08-25T16:00:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [August 25, 2017, 4:00pm UTC](https://forum.elixirforum.com/t/how-does-protocol-dispatch-work/8114/1 "2017-08-25T16:00:11Z")

</div>

For example, how does `Kernel`‘s `to_string` “know” that it belongs to `String.Chars`?

If I do a `defimpl String.Chars, for: MyMod` – how does `to_string` know  
to call my implemented function?

Just curious.

---

<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 25, 2017, 4:09pm UTC](https://forum.elixirforum.com/t/how-does-protocol-dispatch-work/8114/2 "2017-08-25T16:09:59Z")

</div>

The module `String.Chars` has a function of `to_string`, and the kernel `to_string` just calls `String.Chars.to_string`.

What a protocol does it auto-generate the protocol functions from a list of definitions. So when you `defimpl String.Chars, for: MyMod` or whatever the syntax was then it defines a module, and at protocol consolidation time it scans all the compiled modules to see what is impl’d (just an attribute marker is all it is, of `protocol_impl`, so for your example it would have an attribute on the module of `protocol_impl: [protocol: String.Char, for: MyMod]`), and it uses those to build functions that just test, so in your example it would generate a function of:

```elixir
defmodule String.Chars do
  # ... other stuff
  def to_string(%MyMod{}=v), do: String.Chars.MyMod.to_string(v)
  # ... other stuff
end

```

I have an [enhanced Protocol library](https://hex.pm/packages/protocol_ex) that you can see the code of how it works if curious too. Can also look at the elixir code as well. 🙂

---

<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 25, 2017, 4:12pm UTC](https://forum.elixirforum.com/t/how-does-protocol-dispatch-work/8114/3 "2017-08-25T16:12:43Z")

</div>

This does only explain it roughly, but should be just enough to understand it.

1. `Kernel.to_string/1` is a macro, expanding to `String.Chars.to_string/1`
2. `String.Chars.to_string/1` matches on its argument, and without protocol consolidation it dispatches to `String.Chars.xxx.to_string/1` where `xxx` is the qualified alias of the struct or elixirs internal type. With consolidation those `xxx` implementations are integrated directly into `String.Chars.to_string/1`.

With consolidation it might look like this (simplified):

```elixir
defmodule String.Chars do
  def to_string(list) when is_list(list), do: List.to_string(list)
  def to_string(%Foo{bar: bar}), do: "#Foo<bar: #{bar}>"
end

```

without consilidation more like this:

```elixir
defmodule String.Chars do
  def to_string(list) when is_list(list), do: List.to_string(list)
  def to_string(%struct{} = item), do: :"Elixir.#{ __MODULE__ }.#{struct}" |> apply(:to_string, [item])
end

```
