Starting with Elixir, and knowing that it is a dynamic language, I wondered why are there typespecs. Typespecs allow you to declare custom data types and typed function signatures. After reading Programming Elixir I learned that this information can be used by an Erlang tool called Dialyzer. By their own definition:
Dialyzer is a static analysis tool that identifies software discrepancies such as type errors, unreachable code, unnecessary tests, etc in single Erlang modules or entire (sets of) applications.
Before being able to use it, we need to build a Persistent Lookup Table, or simply PLT. This file is used to store the result of an analysis, and serves as a cache of information about analyzed functions. You should build a PLT against the OTP applications you commonly use. Therefore, I’ll build it using the elixir lib:
I’m creating 3 modules to exemplify its use.
Blah is the one with an typespec for the
blah function. It expects to receive a number (integer or float), and returns a String. Bleh and Blih are users of that function. The former uses it wrongly, by passing a String instead of a number, and the later uses it correctly, passing the integer 1.
After creating the files in your project, you can compile it and run the tool, checking then the reported errors.
As expected, a warning was emitted for
Bleh, but not for
- Erlang, Elixir and a simple Supervisor
- Using xargs to Parallelize Tasks
- Services Version Lock with Docker and Jenkins
- Hot Code Reload in Elixir
- Type Equality Checking at Compile Time in Scala
- Circuit Breakers and retries in Scala with autobreaker
- Trunk Based Development with Multiple Services
- Automatically Build and Deploy the Blog
- Hydroponics Kit Automation
- Integration testing for nginx Routes