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) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset