Adding Type Specifications

Learn how to use type specifications to improve code.

Introduction to type specifications

Type specifications are notations that say what our functions expect and return. In some languages, the compiler uses the type specifications to optimize the code and check its correctness. Elixir is a dynamic language, and the compiler doesn’t use type specifications to optimize our code. However, the Dialyzer tool uses type specifications to do a static check to verify if type usage is correct, catching some hidden bugs. Type specifications are also good for generating documentation, clarifying what is expected in our code. We’ll use type specifications to improve the DungeonCrawl.Room.Trigger.run/2 contract, and we’ll add them to document the expected structures in the function’s arguments and return.

Adding type specifications to the game

Get hands-on with 1400+ tech skills courses.