Scala 相等与类型检查?
是否有统一的方法来执行类型检查的相等性? 不幸的是
val objectA:String = "test"
val objectB:Int = 2
objectA == objectB
,如果 objectB 是 Int 而 objectA 是 String,则相等运算符 == 不会抱怨。 我需要一个像 === 这样的运算符来执行类型检查(我希望它对所有 scala obj 都是统一的)。这样的运营商存在吗?
Is there a uniform method to perform equality with type checking?
Unfortunately
val objectA:String = "test"
val objectB:Int = 2
objectA == objectB
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要查看 scalaz 的 === 类型安全等于 - 它已实现作为在那里输入类。
您还可以观看 Heiko Seeberger 的演讲,他在其中描述了它的实现方式:
http://days2011.scala-lang.org/node/138/275
您还可以在这里找到一些示例:
http://scalaz.github.com/scalaz/scalaz-2.9.1-6.0.4/doc.sxr/scalaz/example/ExampleEqual.scala.html#24187
(在他们使用
≟
方法的示例,但它只是===
的别名)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 提供了这样一个运算符。
Scalaz provides such an operator.
这也是由 ScalaUtils 库提供的:
This is also provided by the ScalaUtils library:
scala dotty(又名 scala 3)有一个名为 Multiversal 的功能平等允许类型安全平等。
下面是简单的 REPL 示例;
Dotty 是 Scala 的下一代编译器 - http://dotty.epfl.ch/#getting-started
注意:
scala dotty(aka scala 3) has a feature called Multiversal Equality which allows type safe equality.
Below is the dotty REPL example;
Dotty is a next generation compiler for Scala - http://dotty.epfl.ch/#getting-started
Note: