4 ms·
Elixir has a specific kind of typechecking: success typing. You can set up the Erlang Dialyzer tool to work with VSCode using the ElixirLS extension. It will wa
by yawaramin 8y ago
Elixir has a specific kind of typechecking: success typing. You can set up the Erlang Dialyzer tool to work with VSCode using the ElixirLS extension. It will warn you about type errors. Success typing means that there is provably a type error and you actually need to fix it, as opposed to the more common form of typechecking wherein the compiler forces you to prove that there isn't a type error.
True to form, when I see a Dialyzer warning and think about it for a bit, I find that I always need to fix the code. The error messages do take some getting used to, though.
- mercer 8y agoIsn't Dialyzer's success typing similar to TypeScript in that it's gradual typing? Or am I muddling up my concepts?
- yawaramin 8y agoIt's similar and they are both considered gradual typing, yes. Dialyzer is 'better' in some ways though. For example, consider this TypeScript: function f(a) { console.log(a.x); } f({}); With the strictest checks turned on ( try it at https://www.typescriptlang.org/play/#src=function%20f(a)%20%7B%0D%0A%20%20%20%20console.log(a.x)%3B%0D%0A%7D%0D%0A%0D%0Af(%7Bx%3A%201%7D)%3B%0D%0A https://www.typescriptlang.org/play/#src=function%20f(a)%20%... ), TypeScript will give you this message: `Parameter 'a' implicitly has an 'any' type.` Now consider the equivalent Elixir: def test2(), do: f(%{}) def f(a), do: IO.inspect(a.x) Dialyzer will give you this message: `Function test2/0 has no local return ... The call 'YourModule':f(#{}) will never return since it differs in the 1st argument from the success typing arguments: (atom() | #{'x':=_, _=>_})` This is why I said the Dialyzer message takes some getting used to :-) But, it just means that `f` needs to be called with either an atom (because you could pass in a module to the function, and modules are modelled as atoms), or with a map object with the `x` key and possibly some other keys.