Search⌘ K

Protocols and Structs

Explore how Elixir implements user-defined types with structs and how protocols enable polymorphic functions. This lesson guides you through creating and inspecting structs, revealing their underlying map structure, and understanding polymorphism through protocols.

We'll cover the following...

Elixir doesn’t have classes, but perhaps surprisingly it does have user-defined types. It pulls this off using structs and a few conventions.

Example

Let’s play with a simple struct. Here’s the definition:

defmodule Blob do 
  defstruct content: nil
end
...