Appendix 2Type Specifications and Type Checking

When we looked at @callback,, we saw that we defined callbacks in terms of their parameter types and return value. For example, we might write

 @callback parse(uri_info :: URI.Info.t) :: URI.Info.t
 @callback default_port() :: integer

The terms URI.Info.t and integer are examples of type specifications. And, as José Valim pointed out to me, the cool thing is that they are implemented (by Yurii Rashkovskii) directly in the Elixir language itself—no special parsing is involved. This is a great illustration of the power of Elixir metaprogramming.

In this appendix we’ll discuss how to specify types in Elixir. But before we do, there’s another question to address: Why bother?

Get Programming Elixir 1.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.