-
-
Save SatoTakeshiX/bb1011e847393dca945b8ea0cbbcb731 to your computer and use it in GitHub Desktop.
パターンマッチの例 #swift2 #CodePiece #swift #swiftlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Gender { | |
case Male, Female | |
} | |
enum TimeSaleResult { | |
case Normal, Morning, Evening | |
} | |
func TimeSale(hour:Int) -> TimeSaleResult { | |
switch hour { | |
case let h where h < 10: | |
return .Morning | |
case let h where 20 < h: | |
return .Evening | |
default: | |
return .Normal | |
} | |
} | |
let gender = Gender.Female | |
let age = 30 | |
let hour = 12 | |
switch (gender, age, hour) { | |
case (.Female, _ , _ ): print("女性割引") | |
case (_ , let age, _ ) where age < 20: print("学生") | |
case (_ , let age, _ ) where 60 < age: print("シニア") | |
case (_ , _ , hour) where hour < 10: print("早朝割引") | |
case (_ , _ , hour) where 19 < hour: print("深夜割引") | |
default: print("通常割引") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment