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

I am watching this video Let’s build a real time chat application with Elixir and Phoenix LiveView - Manning livestream - YouTube 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 Getting Started with Phoenix LiveView - YouTube 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?

4 Likes

That is because he is implementing a Behaviour.

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.

1 Like

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

1 Like

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

4 Likes

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.

7 Likes

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:

1 Like

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

1 Like

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

https://hexdocs.pm/elixir/Behaviour.html

1 Like

So, behaviour is deprecated?

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

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.

You’ve never used a GenServer?

Read this.

1 Like

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:

@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
2 Likes

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).

1 Like

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

1 Like

My bad, what I meant to link was to: Typespecs — Elixir v1.12.3

The module Behaviour is deprecated, not behaviours themselves.