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

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement