斯卡拉平等与类型检查? [英] Scala equality with type checking?
问题描述
是否存在用于执行类型检查相等性的统一方法? 不幸的是
Is there a uniform method to perform equality with type checking? Unfortunately
val objectA:String = "test"
val objectB:Int = 2
objectA == objectB
相等运算符==不会抱怨objectB是一个Int而objectA是一个String. 我需要像===这样的运算符也执行类型检查(并且我希望它对所有scala obj都是统一的).是否存在这样的运算符?
the equality operator == doesn't complain if objectB is a Int while objectA is a String. I would need an operator like === that perform type checking as well (and I hope it is uniform to all scala obj). Does such operator exist?
推荐答案
您需要查看 scalaz 的
You need to look at scalaz's === for type-safe equals - it's implemented as type class there.
您还可以通过 Heiko Seeberger 观看谈话,他在其中描述了其实现方式:
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:
(in the examples they are using ≟
method, but it's simply alias for ===
)
这篇关于斯卡拉平等与类型检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!