April 2026
Intermediate
683 pages
25h 29m
English
In Chapter 5, you learned about generic functions. You are encouraged to revisit that discussion if you need to as we will reuse that knowledge and syntax intuition here.
Back already? Good! Using a similar syntax as generic functions, enumerations can contain associated values of a generic type—typically symbolized as <T>. Listing 7.32 contains a simple enumeration called TreasureChest, which corresponds to types of chests in a video game.
enum TreasureChest<T> { case empty case treasure(T)}
Listing 7.32 Enumeration with Generic Associated Values
Just like generic functions, this generic enumeration has the suffix <T>, indicating that it is flexible enough to accept different data types. TreasureChest has two ...
Read now
Unlock full access