Scala Range contains(elem: Any) 方法

发布于 2024-12-04 11:39:01 字数 349 浏览 0 评论 0原文

显然 Range 有一个方法可以检查它是否包含 Any 类型的值。我知道它来自 SeqLike,但会导致一些问题。

例如,我正在匹配 joda.DateTime 中的小时:

DateTime.now match {
    case d if 0 to 12 contains d.hourOfDay() => ...

这里 d.hourOfDay() 返回 DateTime.Property,而不是 Int,但代码仍然可以编译,因为 contains(elem: Any)。有什么方法可以在编译时检查此类调用吗?

Apparently Range has a method that checks if it contains a value of type Any. I understand that it is from SeqLike, but causes some problems.

For instance, i was matching hours from joda.DateTime:

DateTime.now match {
    case d if 0 to 12 contains d.hourOfDay() => ...

Here d.hourOfDay() returns DateTime.Property, not Int, but code still compiles, because of contains(elem: Any). Is there any way to check for such calls at compile time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

痴情 2024-12-11 11:39:01

您可以将 Scalaz 的类型安全 equals (===) 与 TraversableOnce 上的 exists 方法结合使用。

scala> import scalaz._
import scalaz._

scala> import Scalaz._
import Scalaz._

scala> 1 to 5 exists { _ === 2 }
res1: Boolean = true

scala> 1 to 5 exists { _ === "Hullo" }
<console>:14: error: type mismatch;
 found   : java.lang.String("Hullo")
 required: Int
       1 to 5 exists { _ === "Hullo" }
                             ^

You can use Scalaz's typesafe equals (===) in conjunction with exists method on TraversableOnce.

scala> import scalaz._
import scalaz._

scala> import Scalaz._
import Scalaz._

scala> 1 to 5 exists { _ === 2 }
res1: Boolean = true

scala> 1 to 5 exists { _ === "Hullo" }
<console>:14: error: type mismatch;
 found   : java.lang.String("Hullo")
 required: Int
       1 to 5 exists { _ === "Hullo" }
                             ^
倾城°AllureLove 2024-12-11 11:39:01

您可以 pimp Range 添加类型-safer contains 方法:

class SafeRange( range: Range ) {
  def safeContains( i: Int ) = range contains i
}

object SafeRange {
  implicit def safer( range: Range ) = new SafeRange( range )
}

导入隐式并在任何范围实例上调用 safeContains

scala> import SafeRange._
import SafeRange._

scala> (0 until 10) safeContains 3
res2: Boolean = true

scala> (0 until 10) safeContains 100
res3: Boolean = false

scala> (0 until 10) safeContains "foo"
<console>:18: error: type mismatch;
 found   : java.lang.String("foo")
 required: Int
          (0 until 10) safeContains

You can pimp Range to add a type-safer contains method:

class SafeRange( range: Range ) {
  def safeContains( i: Int ) = range contains i
}

object SafeRange {
  implicit def safer( range: Range ) = new SafeRange( range )
}

Import the implicit and call safeContains on any range instance:

scala> import SafeRange._
import SafeRange._

scala> (0 until 10) safeContains 3
res2: Boolean = true

scala> (0 until 10) safeContains 100
res3: Boolean = false

scala> (0 until 10) safeContains "foo"
<console>:18: error: type mismatch;
 found   : java.lang.String("foo")
 required: Int
          (0 until 10) safeContains
纵性 2024-12-11 11:39:01

基于 Range 的 scaladocs 它看起来像没有更好的 Range 方法可以使用。您的选择似乎是

使用显式类型签名:

 case d if 0 to 12 contains (d.hourOfDay(): Int) => ...

创建您自己的方法:

 def containsInt(r: Range, i: Int) = ...

这似乎是 Java equals 成为前泛型的延续,并且只是这一事实给 Scala 带来的不便之一。

Based on the scaladocs for Range it looks like there's not a better Range method you could use. Your options seem to be

Use an explicit type signature:

 case d if 0 to 12 contains (d.hourOfDay(): Int) => ...

Make your own method:

 def containsInt(r: Range, i: Int) = ...

This seems to be a holdover from Java equals being pre-generics, and is only one of the inconveniences this fact causes for Scala.

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