Guest User

Untitled

a guest
Jul 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. enum Planet: Int {
  2. case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
  3. // case pluto
  4.  
  5. static var gasGiants: Set<Planet> = [.jupiter, .saturn]
  6. }
  7.  
  8. let planet: Planet = .jupiter
  9.  
  10. if Planet.gasGiants.contains(planet) {
  11. print("\(planet) is a gas giant")
  12. }
  13.  
  14.  
  15. // ============
  16. // alternatively...
  17. // this could be extended generically to all collections of any type,
  18. // but that could cause hidden performance issue if `contains` is not O(1)
  19. func ~=(planets: Set<Planet>, planet: Planet) -> Bool {
  20. return planets.contains(planet)
  21. }
  22.  
  23. switch planet {
  24. case Planet.gasGiants: print("\(planet) is a gas giant")
  25. default: print("I don't know")
  26. }
Add Comment
Please, Sign In to add comment