mix compile.tyx (tyx v0.0.1) View Source
Cross-module type validation.
This compiler reports all type violations.
Usage
Once you have Tyx
used anywhere in your project, you need to include the compiler in mix.exs
:
defmodule MyApp.MixProject do
# ...
def project do
[
compilers: [:tyx | Mix.compilers()],
# ...
]
end
# ...
end
When developing a library, it's advised to use this compiler only in :dev
and :test
environments:
defmodule MyLib.MixProject do
# ...
def project do
[
compilers: extra_compilers(Mix.env()) ++ Mix.compilers(),
# ...
]
end
# ...
defp extra_compilers(:prod), do: []
defp extra_compilers(_env), do: [:tyx]
end
Warnings
Every invalid type is reported as a compiler warning. Consider the following example:
defmodule MyApp.User do
use Tyx
deft auth(name: String, pass: String, OUT: :ok) do
MyApp.Auth.validate(name, pass)
end
end
Assuming that MyApp.Auth.validate/2
might fail returning {:error, _}
tuple,
you'll get the following warning:
$ mix compile
warning: type violation in `MyApp.User.auth/2`
(returned value `{:error, _}` is not allowed)
lib/my_app/user.ex:3
Since the compiler emits warnings, mix compile
will still succeed, and you can normally start
your system, even if some type checks has not succeeded. The compiler doesn’t force you to immediately
fix these type errors, which is a deliberate decision made to avoid disrupting the development flow.
At the same time, it's worth enforcing types on the CI. This can easily be done by providing
the --warnings-as-errors
option to mix compile
.