# Ensuring module implements behaviour

**URL:** <https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703>\
**Category:** Questions\
**Tags:** pattern-matching\
**Created:** [August 22, 2022, 2:18pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703 "2022-08-22T14:18:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aochagavia](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aochagavia/32/27092_2.png) [@aochagavia](https://forum.elixirforum.com/u/aochagavia)\
**Post date:** [August 22, 2022, 2:18pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/1 "2022-08-22T14:18:07Z")

</div>

### Context

A feature of Elixir I find myself using often when defining functions is to pattern match on structs. This way I can ensure the function is being called with the data I expect, get nice autocompletion in my IDE and even get compile errors if I ever modify the names of the fields of the struct and forget to update them at the function definition.

Consider the (utterly simplified) example below:

```elixir
def get_name(%Employee{name: name}) do
  name
end

```

### Question

Sometimes a function takes a module instead of a struct. In those cases, it would be very useful to have a lightweight way of enforcing that the module implements a particular behaviour (either through pattern matching or through guards would be ideal).

An example with hypothetical syntax:

```elixir
def sort(list, sorting_module) when implements_behaviour(sorting_module, SortingAlgorithm) do
  sorting_module.sort(list)
end

```

I can imagine there are more people like me who like to enforce invariants in their code. Hence my question: are there any Elixir idioms to ensure a module implements a behaviour? Or is this somehow not “the Elixir way”?

---

<div class="post-metadata">

**Author:** ![ityonemo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/32/11341_2.png) [@ityonemo](https://forum.elixirforum.com/u/ityonemo)\
**Post date:** [August 22, 2022, 2:40pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/2 "2022-08-22T14:40:56Z")

</div>

There are not idiomatically. I have often thought it would be nice if the module() type could take a parameter declaring which behaviours it implements.

If you want, it is possible to write a function that will go ahead and check that for you, by 1) calling `module.module_info(:behaviours)` and seeing if it’s declared. 2) If you want to be even more paranoid (it’s possible someone ignored that compiler warning) you can also run `function_exported?` on the module to see if it _really_ implemented the functions you hoped for.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [August 22, 2022, 2:43pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/3 "2022-08-22T14:43:52Z")

</div>

There is no way. Well, there may be one hacky way to do so, but it will not provide guarantee that it does it 100% (as we cannot check the _behaviour_ of the implemented functions, only their existence).

Additionally functions like `function_exported?/3` may behave weird in presence of dynamic module loading.

The “Elixir way” would be to simply try to use interface and if the programmer passed wrong value, then it is the error and the process that have done that should be punished (by death).

---

<div class="post-metadata">

**Author:** ![ityonemo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/32/11341_2.png) [@ityonemo](https://forum.elixirforum.com/u/ityonemo)\
**Post date:** [August 22, 2022, 2:46pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/4 "2022-08-22T14:46:17Z")

</div>

> function\_exported?/3 may behave weird in presence of dynamic module loading

I forgot about that – and I even was struggling with that a few days ago. Ironically it’ll work in prod, it’s just in dev you could tear your hair out if you depend on the module… Though if you do it in the order specified (module\_info first) then I believe you are guaranteed to have function\_exportes work.

> process that have done that should be punished (by death).

This is the way 😈

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [August 22, 2022, 3:54pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/5 "2022-08-22T15:54:39Z")

</div>

> [@ityonemo](#):
>
> Ironically it’ll work in prod, it’s just in dev you could tear your hair out if you depend on the module…

There are ways around that as well by using the `Code` module but then you just kind of do prod running of an app in a supposed dev environment. 😃

@aochagavia Check this thread where I helped one nice guy with a similar problem: [Load all modules implementing a behaviour in escript](https://forum.elixirforum.com/t/load-all-modules-implementing-a-behaviour-in-escript/33348)

Maybe it can inspire your own solution to your problem.

---

<div class="post-metadata">

**Author:** ![ityonemo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/32/11341_2.png) [@ityonemo](https://forum.elixirforum.com/u/ityonemo)\
**Post date:** [August 22, 2022, 4:54pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/6 "2022-08-22T16:54:41Z")

</div>

Here is José’s breadcrumb to find the answer, there is a nice solution that is two lines of code. I might come by and post it later if people clamor that they don’t want to figure it for themselves

[Is there a way to tell mix tasks to run in embedded mode?](https://forum.elixirforum.com/t/is-there-a-way-to-tell-mix-tasks-to-run-in-embedded-mode/49645)

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [August 22, 2022, 4:56pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/7 "2022-08-22T16:56:00Z")

</div>

Hm, if that’s an old command-line switch then I’d be really embarrassed that I haven’t checked.

---

<div class="post-metadata">

**Author:** ![al2o3cr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/32/3457_2.png) [@al2o3cr](https://forum.elixirforum.com/u/al2o3cr)\
**Post date:** [August 22, 2022, 5:21pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/8 "2022-08-22T17:21:57Z")

</div>

> [@aochagavia](#):
>
> Hence my question: are there any Elixir idioms to ensure a module implements a behaviour?

Let it crash with an `UndefinedFunctionError`. A behaviour provides a set of callbacks that can be checked with `@impl`, but your `sort/2` function should be fine with _any_ module that defines an appropriate `sort/1`.

Example, from `Enum.sort` that accepts a module name in the second position:

```elixir
iex(1)> Enum.sort([1,2,3], Application)
** (UndefinedFunctionError) function Application.compare/2 is undefined or private
    (elixir 1.13.4) Application.compare(1, 2)
    (elixir 1.13.4) lib/enum.ex:3019: anonymous fn/3 in Enum.to_sort_fun/1
    (stdlib 4.0.1) lists.erl:1022: :lists.sort/2

```

---

<div class="post-metadata">

**Author:** ![1stSolo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/1stsolo/32/30341_2.png) [@1stSolo](https://forum.elixirforum.com/u/1stSolo)\
**Post date:** [December 28, 2022, 8:29pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/9 "2022-12-28T20:29:35Z")

</div>

Can we pattern-match on behavior? For example,

```elixir
def compute_sum(list) when is_list(list) do
  list
  |> Enum.reduce(0, fn item, acc ->
    maybe_add(acc, item)
  )
end

defp maybe_add(Computable = item, acc) do
   acc + Computable.value_of(item)
end
defp maybe_add(_, acc) do
   acc
end

```

or even simpler with `case`:

```elixir
def compute_sum(list) when is_list(list) do
  list
  |> Enum.reduce(0, fn item, acc ->
    case item do
      Computable -> acc + Computable.value_of(item)
      _ -> acc
    end
  )
end

```

Can something of that sort be done?

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [December 28, 2022, 10:31pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/10 "2022-12-28T22:31:17Z")

</div>

No, there is no way to do that

---

<div class="post-metadata">

**Author:** ![1stSolo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/1stsolo/32/30341_2.png) [@1stSolo](https://forum.elixirforum.com/u/1stSolo)\
**Post date:** [December 28, 2022, 11:33pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703/11 "2022-12-28T23:33:26Z")

</div>

This can be done with Protocols:

```elixir
list
|> Enum.reduce(fn x, acc ->
     case Protocol.impl_for(x) do
       nil -> acc
       module -> acc + module.value_of(item)
     end)

```
