Search⌘ K
AI Features

Specifications for Functions and Callbacks

Explore how to specify function parameters, return types, and multiple function heads using the @spec attribute in Elixir. Understand its role in documenting function behavior and improving code clarity.

We'll cover the following...

The @spec attribute specifies a function’s parameter count, types, and return value type. It can appear anywhere in a module that defines the function, but by convention, it sits immediately before the function definition, following any function documentation. We’ve already seen the syntax: @spec function_name( param1_type, ...) :: return_type

Examples

Let’s see ...