- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 372字
- 2021-07-02 23:54:27
Enumerations
In Swift, an enumeration defines a common type for related values and enables us to work with those values in a type-safe way. Values provided for each enumeration member can be a String, Character, Int, or any floating-point type. Enumerations can store associated values of any given type, and the value types can be different for each member of the enumeration, if needed. Enumeration members can come pre-populated with default values (called raw values), which are all of the same type. Consider the following example:
enum MLSTeam {
case montreal
case toronto
case newYork
case columbus
case losAngeles
case seattle
}
let theTeam = MLSTeam.montreal
Enumeration values can be matched with a switch statement, which can be seen in the following example:
switch theTeam {
case .montreal:
print("Montreal Impact")
case .toronto:
print("Toronto FC")
case .newYork:
print("NewyorkRedbulls")
case .columbus:
print("Columbus Crew")
case .losAngeles:
print("LA Galaxy")
case .seattle:
print("Seattle Sounders")
}
Enumerations in Swift are actually algebraic data types that are types created by combining other types. Consider the following example:
enum NHLTeam { case canadiens, senators, rangers, penguins, blackHawks, capitals }
enum Team {
case hockey(NHLTeam)
case soccer(MLSTeam)
}
struct HockeyAndSoccerTeams {
var hockey: NHLTeam
var soccer: MLSTeam
}
The MLSTeam and NHLTeam enumerations each have six potential values. If we combine them, we will have two new types. A Team enumeration can be either NHLTeam or MLSTeam, so it has 12 potential values that are the sum of NHLTeam and MLSTeam potential values. Therefore, Team, an enumeration, is a sum type.
To have a HockeyAndSoccerTeams structure, we need to choose one value for NHLTeam and one for MLSTeam so that it has 36 potential values that are the product of NHLTeam and MLSTeam values. Therefore, HockeyAndSoccerTeams is a product type.
In Swift, an enumeration's option can have multiple values. If it happens to be the only option, then this enumeration becomes a product type. The following example presents an enumeration as a product type:
enum HockeyAndSoccerTeams {
case value(hockey: NHLTeam, soccer: MLSTeam)
}
As we can create sum or product types in Swift, we can say that Swift has first-class support for algebraic data types.
Enumerations and pattern matching will be covered in detail in Chapter 4, Enumerations and Pattern Matching.
- 漫話大數(shù)據(jù)
- Learning Spring Boot
- 分布式數(shù)據(jù)庫系統(tǒng):大數(shù)據(jù)時代新型數(shù)據(jù)庫技術(shù)(第3版)
- 云計算與大數(shù)據(jù)應(yīng)用
- 深入淺出MySQL:數(shù)據(jù)庫開發(fā)、優(yōu)化與管理維護(hù)(第2版)
- The Game Jam Survival Guide
- 數(shù)據(jù)庫技術(shù)及應(yīng)用教程
- 網(wǎng)站數(shù)據(jù)庫技術(shù)
- Hadoop大數(shù)據(jù)開發(fā)案例教程與項(xiàng)目實(shí)戰(zhàn)(在線實(shí)驗(yàn)+在線自測)
- 視覺大數(shù)據(jù)智能分析算法實(shí)戰(zhàn)
- Oracle數(shù)據(jù)庫管理、開發(fā)與實(shí)踐
- Trino權(quán)威指南(原書第2版)
- Learning Libgdx Game Development
- MATLAB基礎(chǔ)及其應(yīng)用教程
- 零基礎(chǔ)學(xué)SQL