是否有一种统一的方法来执行类型检查的相等性?
很遗憾
val objectA:String = "test"
val objectB:Int = 2
objectA == objectB
如果 objectB 是 Int 而 objectA 是 String,则相等运算符 == 不会提示。
我需要一个像 === 这样的运算符来执行类型检查(我希望它对所有 scala obj 都是统一的)。这样的运营商存在吗?
最佳答案
scala dotty(aka scala 3) 有一个特性叫做 Multiversal Equality这允许类型安全相等。
下面是 dotty REPL 示例;
scala> val data1 = "string"
val data1: String = "string"
scala> val data2 = Array(1, 2, 3, 4)
val data2: Array[Int] = [I@86733
scala> val comparisonBool = data1 == data2
1 |val comparisonBool = data1 == data2
| ^^^^^^^^^^^^^^
| Values of types String and Array[Int] cannot be compared with == or !=
Dotty 是下一代 Scala 编译器 - http://dotty.epfl.ch/#getting-startedNote:
When will scala 3 come out?
The intent is to publish the final Scala 3.0 soon after Scala 2.14. At the current release schedule (which might still change), that means early 2020.
关于带有类型检查的Scala相等性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9084464/