O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The algebra of data types

We have seen that enums in Swift are actually algebraic data types. Let's explore some examples to get more familiar with the topic.

The following example presents a simple enum NHLTeam with different options. Playing hockey is the second biggest sport after snow shoveling in Canada so don't get bored by the NHL examples!

The enum Team uses NHLTeam along with MLSTeam that we defined before to combine Hockey and Soccer teams. Team can be either a Hockey NHL team or a Soccer MLS team:

enum NHLTeam {     case canadiens     case senators     case rangers     case penguins     case blackHawks     case capitals } enum MLSTeam {     case montreal     case toronto     case newYork     case columbus     case losAngeles     case seattle } enum Team {  case ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required