# Do I have to write @impl true before each function?

**URL:** <https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036>\
**Category:** Questions\
**Tags:** phoenix\
**Created:** [November 22, 2021, 7:26pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036 "2021-11-22T19:26:17Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalkatfyodor](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/fbc32d/32.png) [@kalkatfyodor](https://forum.elixirforum.com/u/kalkatfyodor)\
**Post date:** [November 22, 2021, 7:26pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/1 "2021-11-22T19:26:17Z")

</div>

I am watching this video [https://youtu.be/\_rTFZbvMfJA?t=1252](https://youtu.be/_rTFZbvMfJA?t=1252) and the presenter is saying that putting @impl true offers some better error handling - but doesn’t go deeper. In other tutorials like this one [https://youtu.be/x1rx4-AAcMQ?t=346](https://youtu.be/x1rx4-AAcMQ?t=346) where they don’t put @impl true before mount, render and handle functions.

Should I put @impl true before each function in Phoenix or not?

---

<div class="post-metadata">

**Author:** ![tomkonidas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomkonidas/32/22982_2.png) [@tomkonidas](https://forum.elixirforum.com/u/tomkonidas)\
**Post date:** [November 22, 2021, 7:45pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/2 "2021-11-22T19:45:26Z")

</div>

That is because he is implementing a [Behaviour](https://elixir-lang.org/getting-started/typespecs-and-behaviours.html#behaviours).

No, you do not need it for every function. But if you put on one function in the module,  
then it will complain to put on the others.

---

<div class="post-metadata">

**Author:** ![kalkatfyodor](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/fbc32d/32.png) [@kalkatfyodor](https://forum.elixirforum.com/u/kalkatfyodor)\
**Post date:** [November 22, 2021, 7:57pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/3 "2021-11-22T19:57:51Z")

</div>

I have no idea what you are talking about but thank you anyway.

---

<div class="post-metadata">

**Author:** ![gabriel137](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gabriel137/32/18142_2.png) [@gabriel137](https://forum.elixirforum.com/u/gabriel137)\
**Post date:** [November 22, 2021, 8:09pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/4 "2021-11-22T20:09:56Z")

</div>

This feature informs the terminal which functions are returned from a call like in GenServer. You don’t need to include them at all, just the first one.

To learn more, view the link below, I recommend that you first understand what GenServers are.

[https://elixircasts.io/%40impl-attribute](https://elixircasts.io/%40impl-attribute)

---

<div class="post-metadata">

**Author:** ![tomkonidas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomkonidas/32/22982_2.png) [@tomkonidas](https://forum.elixirforum.com/u/tomkonidas)\
**Post date:** [November 22, 2021, 8:11pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/5 "2021-11-22T20:11:16Z")

</div>

So you can define a behaviour and implement it. For example, lets use payment provider.

If we know we will have multiple payment providers (ex: Stripe, Paypal, etc…), we might want a behaviour for payment providers. We can do this with a behaviour.  
We can define the behaviour and say that every module that implements the MyApp.PaymentProvider behaviour needs to implement a `credit/2` and `debit/2` for example.

Now in our own Stripe module we can `@behaviour MyApp.PaymentProvider` and that is saying that we will need to implement the 2 callbacks we defined:

- `debit/2`
- `credit/2`

Now we have a way to make sure we are able to switch between Paypal and Stripe because they follow the same contract. Both `Paypal.credit/2` and `Stripe.credit/2` should take the same args and return the same type/structure.

With Phoenix, they have optional callbacks, You can define your own or use the default. That is why you do not need to implement `Phoenix.LiveComponent.update/2`, but you can impl it if you want.

In your module, if you put one `@impl` over a function, I believe the compiler will complain. Because you iplemented it for another callback but not that specific one, so it thinks you made a possible mistake.

---

<div class="post-metadata">

**Author:** ![eksperimental](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/32/1401_2.png) [@eksperimental](https://forum.elixirforum.com/u/eksperimental)\
**Post date:** [November 23, 2021, 3:11am UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/6 "2021-11-23T03:11:35Z")

</div>

It is good practice.  
Even better, to use the name of the behaviour module that you are implementing, instead of just `true`. This is necessary when a module implement more than one behaviour.

It will definitely improve the readability and maintainability of your code.

Read more about @impl and behaviours in:

- [Module — Elixir v1.20.2](https://hexdocs.pm/elixir/Module.html#module-impl)
- [Behaviour — Elixir v1.20.2](https://hexdocs.pm/elixir/Behaviour.html)
- [https://elixir-lang.org/getting-started/typespecs-and-behaviours.html#behaviours](https://elixir-lang.org/getting-started/typespecs-and-behaviours.html#behaviours)

---

<div class="post-metadata">

**Author:** ![eksperimental](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/32/1401_2.png) [@eksperimental](https://forum.elixirforum.com/u/eksperimental)\
**Post date:** [November 23, 2021, 3:12am UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/7 "2021-11-23T03:12:43Z")

</div>

> [@kalkatfyodor](#):
>
> I have no idea what you are talking about but thank you anyway.

You need to understand the concept of behaviours first in order to understand what @impl is for.

---

<div class="post-metadata">

**Author:** ![kalkatfyodor](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/fbc32d/32.png) [@kalkatfyodor](https://forum.elixirforum.com/u/kalkatfyodor)\
**Post date:** [November 23, 2021, 9:49am UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/8 "2021-11-23T09:49:07Z")

</div>

> his module is deprecated. Use @callback and @macrocallback attributes instead.

> **[Behaviour — Elixir v1.20.2](https://elixir.hexdocs.pm/Behaviour.html)**

---

<div class="post-metadata">

**Author:** ![kalkatfyodor](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/fbc32d/32.png) [@kalkatfyodor](https://forum.elixirforum.com/u/kalkatfyodor)\
**Post date:** [November 23, 2021, 9:49am UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/9 "2021-11-23T09:49:24Z")

</div>

So, behaviour is deprecated?

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [November 23, 2021, 10:37am UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/10 "2021-11-23T10:37:08Z")

</div>

The functionality of behaviours is still existing, though the old api is deperectated in favor of the newer one.

---

<div class="post-metadata">

**Author:** ![kalkatfyodor](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/fbc32d/32.png) [@kalkatfyodor](https://forum.elixirforum.com/u/kalkatfyodor)\
**Post date:** [November 23, 2021, 10:38am UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/11 "2021-11-23T10:38:47Z")

</div>

Can you give a real world example about this behavior? Have you ever used it? How? And what problem did it solve? I can’t wrap my head around it. I have never needed something like that.

---

<div class="post-metadata">

**Author:** ![cmo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmo/32/19618_2.png) [@cmo](https://forum.elixirforum.com/u/cmo)\
**Post date:** [November 23, 2021, 11:06am UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/12 "2021-11-23T11:06:17Z")

</div>

You’ve never used a GenServer?

Read [this](https://www.erlang.org/doc/design_principles/des_princ.html#behaviours).

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [November 23, 2021, 3:33pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/13 "2021-11-23T15:33:47Z")

</div>

I have a feeling you are brand new to this language (apologies if I’m wrong) and perhaps these answers are overcomplicating things (no disrespect to anyone trying to help here!)

The short answer is that you can choose to annotate your callback implementations (in the case of LiveView: `render/1`, `mount/3`, `handle_event/3`, etc) with `@impl true`. Without worrying about anything else, the absolute simplest thing this accomplishes is letting anyone reading the file know that these are callbacks. While it is optional, I highly recommend it.

@tomkonidas provided a very good explanation of behaviours (which you are now well aware is what `@impl true` is tied to) but my one-line definition is: “a set of functions a module must implement”. As a starting point, you can think of behaviours like interfaces from OO even though it’s not strictly true.

Also, when defining multiple versions of the same function, you only need to annotate the first one:

```elixir
@impl true
def handle_event("update_post", params, socket) do
  {:noreply, socket}
end

# not required here!
def handle_event("delete_post", params, socket) do
  {:noreply, socket}
end

```

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [November 23, 2021, 3:42pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/14 "2021-11-23T15:42:12Z")

</div>

Because those are not multiple versions of the same function, those are clauses of a single function.

(I don’t want to nit-pick here, it’s just that it makes more sense once you know it).

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [November 23, 2021, 4:15pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/15 "2021-11-23T16:15:26Z")

</div>

Oh absolutely, it’s a good nitpick (ie, not a nitpick).

---

<div class="post-metadata">

**Author:** ![eksperimental](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/32/1401_2.png) [@eksperimental](https://forum.elixirforum.com/u/eksperimental)\
**Post date:** [November 23, 2021, 7:13pm UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/16 "2021-11-23T19:13:09Z")

</div>

> [@kalkatfyodor](#):
>
> > his module is deprecated. Use @callback and @macrocallback attributes instead.
> 
> [https://hexdocs.pm/elixir/Behaviour.html](https://hexdocs.pm/elixir/Behaviour.html)

My bad, what I meant to link was to: [Typespecs reference — Elixir v1.20.2](https://hexdocs.pm/elixir/typespecs.html#behaviours)

The module `Behaviour` is deprecated, not behaviours themselves.

---

<div class="post-metadata">

**Author:** ![whathappenedtoicabod](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/whathappenedtoicabod/32/38527_2.png) [@whathappenedtoicabod](https://forum.elixirforum.com/u/whathappenedtoicabod)\
**Post date:** [July 23, 2025, 1:24am UTC](https://forum.elixirforum.com/t/do-i-have-to-write-impl-true-before-each-function/44036/18 "2025-07-23T01:24:02Z")

</div>

This is all I’ve ever came across about it [@impl true](https://hexdocs.pm/elixir/genservers.html#:~:text=we%20have%20added-,%40impl%20true,-before%20each%20callback)
