tim2CF
How to make Dialyzer more strict?
If we have function with typespec that always returns value with incorrect type
@spec hello(integer()) :: map()
def hello(int) do
case int do
1 -> :foo
_ -> :wtf
end
end
then Dialyzer generates warning, good. It is as expected.
done in 0m1.08s
lib/hello.ex:16: Invalid type specification for function 'Elixir.Hello':hello/1. The success typing is (_) -> 'foo' | 'wtf'
done (warnings were emitted)
But if
@spec hello(integer()) :: map()
def hello(int) do
case int do
1 -> %{hello: "world"}
_ -> :wtf
end
end
then
done in 0m1.07s
done (passed successfully)
Why it not generates any warnings when there is clause where wrong value is returned from function?
And if it is feature, not bug - how to make behaviour more strict?
It would be perfect if there will be flag to generate warnings when at least one possible clause has incorrect type.
Marked As Solved
losvedir
Came across this discussion when searching for Elixir types / dialyzer stuff. I think it’s worth mentioning that with the latest dialyxir (1.0.0-rc7) and Erlang/OTP 22.2, and the overspecs flag that Jose mentioned, dialyzer does catch the error as expected with the example:
lib/test_types.ex:15: The success typing for 'Elixir.TestTypes':hello/1 implies that the function might also return 'wtf' but the specification return is map()
Unfortunately, overspecs is noisy and fails, for example this function and spec:
@spec hi(integer()) :: :foo | :bar
def hi(n) do
if n < 5 do
:foo
else
:bar
end
end
with:
Type specification is a subtype of the success typing.
Function:
TestTypes.hi/1
Type specification:
@spec hi(integer()) :: :foo | :bar
Success typing:
@spec hi(_) :: :bar | :foo
You can fix it by adding def hi(n) when is_integer(n) do.
So it seems like overspecs might be something you could add when you’re starting a project, but hard to enable after the fact.
Also Liked
rvirding
The reason for this is that dialyzer was added afterwards to a very dynamically typed language, then Erlang and now Elixir, with the requirement that it was not allowed to change the language. At all! So it does what it can. You will find that it doen’t just type a function but type checks a function and the calls made to it together to see if they agree.
Also in some ways dialyzer basically ignores a function spec when type checking so you don’t really need one to get the type checking. If you give one it will check that it is consistent with what it can work out about the function and it will use it when reporting type errors, which can be very useful. Actually same with type defs. Though both are extremely useful for documentation.
The compiler always ignores type specs except for some basic syntax checks.
Depressing no? ![]()
josevalim
Did you try the overspecs/underspecs flags? dialyzer — OTP 29.0.2 (dialyzer 6.0.1)
Dialyzer is fine for the trade-offs it chose to make. Having something more strict most likely means rejecting code that is completely valid today. Or in the best case scenario, forcing types to be explicit and not relying on inference.
@tmbb is correct here. There aren’t low hanging fruits when it comes to types. Adding a type system involves many compromises and the compromises you are willing to make change a lot when the language already exists.
If you want to have type inference, then many Elixir idioms will no longer be supported. Elixir code is based on unions (it returns {:ok, _} | :error) and intersections (different clauses of a function) and those are very expensive for an inference engine.
We could add requirements, such as obligatory typing of inputs, but that doesn’t solve the problem that we have both typed and non-typed code and we should likely check the values at the boundaries, adding runtime cost.
If you want to keep the language the same and support all of the existing idioms and also not worry about conversion costs: that’s Dialyzer.
Long story short: we could have other type systems but you need to start from scratch and you will have to compromise in many ways.
Apologies but I did not mean that Clojure Spec is the future. It is unlikely we will have something like Clojure Spec in Elixir per-se, especially because we have typespecs and guards and adding a third option is going to bring only marginal gains. If somebody wants to explore something like Clojure Spec as a package, then please do!
stefanchrobot
Question is: can this be improved in the future? TypeScript is doing a similar thing for JavaScript. On the bright side, the coverage of different use cases is increasing while keeping the compilation time fast. The bad thing is that IMO the type system is kind of exploding with complexity.
Seems like Elixir could add opt-in strict type checking.
Popular in Questions
Other popular topics
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









