Hi folks,
I want to create a function spec which tells the compiler that the return type will be conforming to a given Behavior. This question was asked several times many years ago, here and e.g. on StackOverflow: elixir - How to use typespecs and Dialyzer with Behaviours? - Stack Overflow
I am re-asking (with the exact same title as SO) as I want to get reassurance whether this problem is now solved with dialyzer nowadays. Back then at SO, this returned a dialyzer error:
@spec greeter() :: GreeterBehaviour # This doesn't work with dialyzer
def greeter do
FormalGreeter # Can easily be swapped to CasualGreeter
end
# lib/my_app.ex:19: Invalid type specification for function 'Elixir.MyApp':greeter/0. The success typing is () -> 'Elixir.FormalGreeter'
Nowadays, using Elixir 1.18.2
and dialyzer 1.4.3
, I don’t get such error message, is this problem solved nowadays - while there is no error message thrown, I don’t get type inference in LSP, thus my question. I am unsure whether this spec literally returns the GreeterBehaviour
module or "a module conforming to GreeterBehaviour
.
Thanks!