[[Elixirで静的型解析]] を参考に初期設定。
以下のコードを書いて
```elixir
defmodule MyProject do
def hello do
:world
end
@spec sum(a :: String.t(), b :: String.t()) :: integer
def sum(a, b) do
a + b
end
end
IO.puts(MyProject.sum(1, 1))
```
実行してみたらerrorsになった。
```console
$ mix dialyzer
Compiling 1 file (.ex)
2
Finding suitable PLTs
Checking PLT...
[:compiler, :elixir, :kernel, :logger, :stdlib]
PLT is up to date!
No :ignore_warnings opt specified in mix.exs and default does not exist.
Starting Dialyzer
[
check_plt: false,
init_plt: ~c"c:/Users/syoum/tmp/my_project/_build/dev/dialyxir_erlang-25.3.2.4_elixir-1.15.4_deps-dev.plt",
files: [~c"c:/Users/syoum/tmp/my_project/_build/dev/lib/my_project/ebin/Elixir.MyProject.beam"],
warnings: [:unknown]
]
Total errors: 1, Skipped: 0, Unnecessary Skips: 0
done in 0m0.64s
lib/my_project.ex:6:invalid_contract
The @spec for the function does not match the success typing of the function.
Function:
MyProject.sum/2
Success typing:
@spec sum(number(), number()) :: number()
________________________________________________________________________________
done (warnings were emitted)
Halting VM with exit status 2
```
[[Neovim]]でもインラインでwarningになってそう。
## 解析されないケース
以下は型エラーにならない。。
```elixir
defmodule MyProject do
@spec print(String.t()) :: no_return()
def print(a) do
IO.puts(a)
end
end
MyProject.print(1)
```
[[Dialyzerは楽観的な型推論を行う]]ためと思われる。