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.

sodapopcan

sodapopcan

It’s worth noting that another benefit of @impl is to signal to the programmer that something is, indeed, a callback implementation. This isn’t as apparent in something as ubiquitous as a GenServer, but when reading a module implementing a less well-known or custom behaviour, this is really nice-to-have info.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement