Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Safe_==[T](value: T) {
- def safe_==(other: T) = value == other
- }
- implicit def toSafe_==[T](value: T) = new Safe_==(value)
- scala> 5 == "test"
- <console>:10: warning: comparing values of types Int and java.lang.String using `==' will always yield false
- 5 == "test"
- ^
- res0: Boolean = false
- scala> 5 safe_== "test"
- <console>:10: error: type mismatch;
- found : java.lang.String("test")
- required: Int
- 5 safe_== "test"
- ^
- scala> "hello" safe_== "test"
- res2: Boolean = false
- scala> "hello" safe_== "hello"
- res3: Boolean = true
- // But it goes wrong when inheritance is involved
- scala> val o: Option[String] = None
- o: Option[String] = None
- scala> o safe_== None
- res3: Boolean = true
- scala> None safe_== o
- <console>:11: error: type mismatch;
- found : o.type (with underlying type Option[String])
- required: None.type
- None safe_== o
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement