First what is Gradual typing?
Gradual typing is a type system that lies in between static typing and in dynamic typing.
In other words gradual typing is a way to add type checks at compile to a dynamic language.
So how does that look in the elixir language?
Currently this is under development as of last year and there are a few steps the core developers are targeting:
- In the first release, types will be used just internally by the compiler
- The second milestone is to introduce type annotations only in structs
- The third milestone is to introduce the (most likely) $-prefixed type annotations for function
You can read more about each of these phases at the blog post that introduced Gradual typing for elixir: here https://elixir-lang.org/blog/2023/06/22/type-system-updates-research-dev/
Ok so now that we have a general idea what can we do today with it?
For that we are going to take a look at the changelogs from the 1.17.3 release. https://github.com/elixir-lang/elixir/blob/v1.17.3/CHANGELOG.md For the most part not a lot as yet but there has been some new things added. At the moment in 1.17 it looks we got type inference
Say we have
defmodule Sample do
def foo do
max = "100"
999 < max
end
end
When compiled you will get a warning.
warning: comparison between incompatible types found:
999 < max
where "max" was given the type:
# type: binary()
# from: iex:3
max = "100"
While Elixir can compare across all types, you are comparing
across types which are always distinct, and the result is either
always true or always false
└─ iex:4: Sample.foo/0
This is awesome!
While this is just the very start and we are just now getting a idea to what is to come, I think this is huge step forward to arming developers with even more tools for coding