'What is Dialyzer’s success typing in Elixir?

Before asking, I have tried to search on Google to understand it, Because Elixir is not widely used, The number of articles is less. The articles which describe Success typing give too abstract information making it difficult for me to understand.

Can someone please help me to understand it in detail with an example? if possible, please



Solution 1:[1]

Dialyzer and Elixir's use of types and specs is perhaps one of the most confusing things about the language. Elixir is unusual because it is a compiled language that is dynamically typed (!!). The "looseness" of the variable typing is by design: having flexibility there is one of the things that helps you do hot-code swapping (which was one of the things for which Erlang was designed).

Support for type-specs was added after-the-fact. These declarations are non-essential decorations on top of the functional code -- they don't need to be there, and they don't necessarily need to be totally accurate, which makes them confusing for those of us who have familiarity with strongly-typed languages where any loose interpretation is not tolerated.

Dialyzer offers a way to evaluate the correctness of the function typing -- it can help identify places where perhaps the listed specs are not accurate. Again, what's confusing here is that this process isn't as strict as we as programmers might expect: sometimes Dialyzer complains about things that are working fine, and sometimes it sees no errors where things are problematic (although its accuracy has improved a lot IMO in the past year alone).

To make sense of this, I find it helpful to remember the purpose of the specs/annotations: they help communicate to humans what a function accepts and returns (the downstream benefits this provides to the compiler I regard as secondary). So just remember that if you choose to add success-typing to your functions, go slowly and check with Dialyzer often -- any errors that it comes up with should be seen as a disagreement between what YOU say the code should receive and return vs. what Dialyzer sees the code ACTUALLY doing. If you don't define any specs, there is no disagreement. Combing through your code helps you identify all the possible values that a function might return and Dialyzer is a really good instrument for helping you verify that what you say your function does is what it is actually capable of.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Everett