Scala 相等与类型检查?

发布于 2025-01-01 15:54:35 字数 247 浏览 1 评论 0原文

是否有统一的方法来执行类型检查的相等性? 不幸的是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神也荒唐 2025-01-08 15:54:35

您需要查看 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 ===)

以可爱出名 2025-01-08 15:54:35

Scalaz 提供了这样一个运算符。

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

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
可是我不能没有你 2025-01-08 15:54:35

这也是由 ScalaUtils 库提供的:

import org.scalautils.TypeCheckedTripleEquals._

scala> "Scala" == Some("Scala")
res1: Boolean = false

scala> "Scala" === Some("Scala")
<console>:11: error: types String and Some[String] do not adhere to the type
  constraint selected for the === and !== operators; the missing implicit 
  parameter is of type org.scalautils.Constraint[String,Some[String]]
              "Scala" === Some("Scala")
                      ^

This is also provided by the ScalaUtils library:

import org.scalautils.TypeCheckedTripleEquals._

scala> "Scala" == Some("Scala")
res1: Boolean = false

scala> "Scala" === Some("Scala")
<console>:11: error: types String and Some[String] do not adhere to the type
  constraint selected for the === and !== operators; the missing implicit 
  parameter is of type org.scalautils.Constraint[String,Some[String]]
              "Scala" === Some("Scala")
                      ^
甜是你 2025-01-08 15:54:35

scala dotty(又名 scala 3)有一个名为 Multiversal 的功能平等允许类型安全平等。

下面是简单的 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-started

注意:

scala 3什么时候发布?

目的是发布最终的 Scala 3.0
Scala 2.14 发布后不久。按照当前的发布时间表(可能
仍在变化),这意味着 2020 年初。

scala dotty(aka scala 3) has a feature called Multiversal Equality which allows type safe equality.

Below is the dotty REPL example;

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 is a next generation compiler for Scala - http://dotty.epfl.ch/#getting-started

Note:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文