anagrom_ataf

anagrom_ataf

@impl-attribute on multi-clause functions

When it comes to implementing callbacks of a behaviour (e.g., GenServer) I always struggle whether I should add the @impl-attribute to all function clauses or just to the first.

def Worker do
  use GenServer

  @impl true
  def handle_call(:foo, from, state) do
    # ..
  end
  
  @impl true
  def handle_call(:bar, from, state) do
    # ..
  end
end

What do you think? What are the pros and cons of one way or the other?

Most Liked

brettbeatty

brettbeatty

It’s a little weird, but my personal style is to put the @impl on a standalone function head (when defining multiple clauses for the function).

def Worker do
  use GenServer

  @impl GenServer
  def handle_call(request, from, state)

  def handle_call(:foo, from, state) do
    # ..
  end
  
  def handle_call(:bar, from, state) do
    # ..
  end
end

I guess in my mind it suggests I’m about to define multiple clauses for the same function as opposed to recognizing multiple definitions happen to be for the same function.

sodapopcan

sodapopcan

Only the first one is necessary.

Marcus

Marcus

Hello, I just add @impl true just to the first function. The benefit of @impl is to get warnings from the compiler and this will be reached with one annotation.

Last Post!

anagrom_ataf

anagrom_ataf

Thanks for your feedback.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement