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
Popular in Questions
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New







