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?
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" }
^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With