Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Range contains(elem: Any) method

Tags:

scala

scalac

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?

like image 645
F0RR Avatar asked Dec 04 '22 20:12

F0RR


1 Answers

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" }
                             ^
like image 62
missingfaktor Avatar answered Dec 19 '22 23:12

missingfaktor