fireproofsocks

fireproofsocks

Should using @impl true cause docs to show the callback spec?

I found an old post discussing @impl and @spec: @behaviour, @callback and @spec - #13 by Eiji

I couldn’t tell where this discussion left off.

When I define a @callback, it defines the spec that implementations should follow. So it feels redundant to add a @spec declaration to a function that already has @impl. I would think that saying @impl true is sufficient or even preferable (in a DRY sort of way).

What I’m noticing is in my generated docs, functions that implement a callback do not show a @spec unless it is added explicitly. (And this means more or less copying the @callback definition).

Is this by design? What is the thinking around this, and how does this square with the DRY ideal of not duplicating work?

Thanks for any input!

Most Liked

Eiji

Eiji

Added 2 issues:

al2o3cr

al2o3cr

FWIW, searching the core Elixir repo for @impl true shows a lot of implementations that are @moduledoc false or at least have no @doc annotation.

Calendar.ISO does the repeating-spec thing:

Eiji

Eiji

The result of mentioned post was an issue:

with José Valim solution:

Currently when you write this code:

defmodule Example do
  @doc "foo"
  @callback foo() :: :ok
end

defmodule Sample do
  @behaviour Example

  def foo, do: :ok
end

You have:

$ iex -S mix
Erlang/OTP 24 [erts-12.3.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Interactive Elixir (1.13.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> b Example.foo
@callback foo() :: :ok

foo

iex(2)> h Sample.foo

                                   def foo()                                    

Sample.foo/0 has no docs but is a callback for behaviour Example. Showing
callback docs instead.

@callback foo() :: :ok

foo

ex_doc shows:

Callback implementation for Example.foo/0

with a link to callback documentation.

Having in mind above you do not need to write a spec. However if you write any @doc to your implementation then you need to note c:Module.callback/arity, so for example in ex_doc you can quickly navigate to said callback to see it’s @spec. Also modern editors are able to show same docs in popup. Therefore you do not need to write @spec on yourself.

However there is something weird if you add @impl true or @imple Example to Sample.foo/0 implementation. There are no documentation both in iex and ex_doc, so maybe it at least was intended. It’s worth to open an issue for that.

fireproofsocks

fireproofsocks

Thanks for filing those – they both were immediately closed, so I guess this behavior is intentional, but I can’t follow the reasoning as far as the spec is concerned. I still expect to see the original callback spec for functions that are implementing a callback.

al2o3cr

al2o3cr

I feel like there’s a tension between two potential uses for @behaviour:

  • the original meaning, where it defined callbacks for mostly-internal use. For instance, you likely don’t want to see handle_call heads in the documentation for a GenServer

  • a new meaning, where it defines an interface for Mox et al that are used externally. I’ve seen this done with a single module that declared @callbacks and @impled them, where the only “alternative implementation” was in MIX_ENV=test.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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

We're in Beta

About us Mission Statement