kalkatfyodor

kalkatfyodor

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

I am watching this video 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 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?

Most Liked

sodapopcan

sodapopcan

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
tomkonidas

tomkonidas

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.

11
Post #5
gabriel137

gabriel137

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

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement