Using implicits is, of course, easy:
implicit def enrichOptionInt(self: Option[Int]) = new {
def <(i: Int) = self.exists(_ < i)
}
val test1 = x < 3 && o1 < 5
Or, if you want it to work for any type of Numeric:
class EnrichedOptionNumeric[N: Numeric](self: Option[N]) {
def <(n: N) = self.exists(v => implicitly[Numeric[N]].lt(v, n))
}
implicit def enrichOptionNumeric[N: Numeric](self: Option[N]) = new EnrichedOptionNumeric(self)
val oD = Some(2.0)
val test1 = x < 3 && o1 < 5 // true
val testD = x < 3 && oD < 5.0 // true
EDIT to answer the question in the comment:
, , , ==, Option. ( ) , , Scala , .
, "option equals". , ===. , EnrichedOptionNumeric :
def ===(n: N) = self.exists(v => implicitly[Numeric[N]].equiv(v, n))
:
val testE = x < 3 && o1 === 1 // true