Advertisement
vadipp

safe_==

Nov 28th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. class Safe_==[T](value: T) {
  2. def safe_==(other: T) = value == other
  3. }
  4. implicit def toSafe_==[T](value: T) = new Safe_==(value)
  5.  
  6. scala> 5 == "test"
  7. <console>:10: warning: comparing values of types Int and java.lang.String using `==' will always yield false
  8. 5 == "test"
  9. ^
  10. res0: Boolean = false
  11.  
  12. scala> 5 safe_== "test"
  13. <console>:10: error: type mismatch;
  14. found : java.lang.String("test")
  15. required: Int
  16. 5 safe_== "test"
  17. ^
  18.  
  19. scala> "hello" safe_== "test"
  20. res2: Boolean = false
  21.  
  22. scala> "hello" safe_== "hello"
  23. res3: Boolean = true
  24.  
  25. // But it goes wrong when inheritance is involved
  26. scala> val o: Option[String] = None
  27. o: Option[String] = None
  28.  
  29. scala> o safe_== None
  30. res3: Boolean = true
  31.  
  32. scala> None safe_== o
  33. <console>:11: error: type mismatch;
  34. found : o.type (with underlying type Option[String])
  35. required: None.type
  36. None safe_== o
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement