Scala equality with type checking?

You need to look at scalaz's === for type-safe equals - it's implemented as type class there.

You can also watch talk by Heiko Seeberger, where he describes how it's implemented:

http://days2011.scala-lang.org/node/138/275

You can also find some examples here:

http://scalaz.github.com/scalaz/scalaz-2.9.1-6.0.4/doc.sxr/scalaz/example/ExampleEqual.scala.html#24187

(in the examples they are using method, but it's simply alias for ===)


Scalaz provides such an operator.

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

scala> 4 === "Scala"
<console>:14: error: type mismatch;
 found   : java.lang.String("Scala")
 required: Int
              4 === "Scala"
                    ^

scala> 4 === 4
res7: Boolean = true

scala> 4 === 5
res8: Boolean = false