Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Planet: Int {
- case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
- // case pluto
- static var gasGiants: Set<Planet> = [.jupiter, .saturn]
- }
- let planet: Planet = .jupiter
- if Planet.gasGiants.contains(planet) {
- print("\(planet) is a gas giant")
- }
- // ============
- // alternatively...
- // this could be extended generically to all collections of any type,
- // but that could cause hidden performance issue if `contains` is not O(1)
- func ~=(planets: Set<Planet>, planet: Planet) -> Bool {
- return planets.contains(planet)
- }
- switch planet {
- case Planet.gasGiants: print("\(planet) is a gas giant")
- default: print("I don't know")
- }
Add Comment
Please, Sign In to add comment