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
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
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
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
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
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
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
- #api
- #forms
- #metaprogramming
- #security
- #hex










