Skip to content

Instantly share code, notes, and snippets.

@akio0911
Created March 13, 2016 01:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akio0911/4b2cdbc37e1be6510fd4 to your computer and use it in GitHub Desktop.
Save akio0911/4b2cdbc37e1be6510fd4 to your computer and use it in GitHub Desktop.
パターンマッチの例 #swift2 #CodePiece #swift #swiftlang
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