madeinussr

madeinussr

Behaviour and Typespec

Hi, all,

Mb it’s silly question :pensive:, but: is there any possibility to define a behaviour in a function’s spec?

for example we have:

defmodule SomeBehaviour do
  @callback do_it() :: any()
end

defmodule Adopter1 do
  @behaviour SomeBehaviour

  def do_it(), do: :done
end

defmodule Adopter2 do
  @behaviour SomeBehaviour

  def do_it(), do: :not_done
end

defmodule SomeModule do
  # So, what's the right @spec for this function, if I'd like to specify SomeBehaviour adopters usage?
  # The first thought is to use just SomeBehaviour.t with @type t :: __MODULE__ in SomeBehaviour definition,
  # but as for me it seems too strict and any adopter will not pass the check
  def do_it_with(adopter), do: adopter.do_it()
end

Thanks.

Marked As Solved

michalmuskala

michalmuskala

Probably the best you can do is to spec it as module - the type system is not powerful enough to express something more advanced.

The idea to add @type t :: module in the behaviour is probably a good one. While it’s still the same when checked with dialyzer, it expresses much more when treating type specs as documentation.

Also Liked

madeinussr

madeinussr

Unfortunately @type t :: module + @spec(SomeBehaviour.t) will check just for any module, not a behaviour adopter. :frowning: well, let it be

Last Post!

madeinussr

madeinussr

Unfortunately @type t :: module + @spec(SomeBehaviour.t) will check just for any module, not a behaviour adopter. :frowning: well, let it be

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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

We're in Beta

About us Mission Statement