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

Composite types with variants

Algebraic data types can be composite types with variants as well. We could create an enum named Dimension to hold the length and width. We can express this enum in both us feet and metric meters. In Swift, we can define such an enum as follows:

enum Dimension {     case us(Double, Double)     case metric(Double, Double) } 

Then we can use the Dimension enumeration to create a variable as follows:

let sizeMetric = Dimension.metric(5.0, 4.0) 

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