Designing Entities with Structs

Programming language entities and data types are not enough when you’re building a larger application. We need to create new data entities to express the application domain. In Elixir, we employ structs. We covered how to use them in Matching Structs, but now we’ll see how to create them. We’ll create a struct that represents the player’s hero. Then we’ll organize our application, separating the domain entities from CLI code by listing the heroes’ options to the player. Then we’ll use some Mix command-line functions to let the player choose a hero. The first step is to build a struct to describe the Character domain entity.

Creating the Character with Structs

We’ll build a struct that will hold all the character’s ...

Get Learn Functional Programming with Elixir 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.