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

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
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 31525 112
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement