madeinussr
Behaviour and Typespec
Hi, all,
Mb it’s silly question
, 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
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.
6
Also Liked
madeinussr
Unfortunately @type t :: module + @spec(SomeBehaviour.t) will check just for any module, not a behaviour adopter.
well, let it be
2
Last Post!
madeinussr
Unfortunately @type t :: module + @spec(SomeBehaviour.t) will check just for any module, not a behaviour adopter.
well, let it be
2
Popular in Questions
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Hello, how can I check the Phoenix version ?
Thanks !
New
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
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
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









