Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala guarded pattern with or matching

I would like to do a pattern match that looks like :

    sinceOp match {
        case  None |Some(lastUpdate) if lastUpdate<= update.time =>

Saddly this does not work. Any ideas ?

Thanks

like image 312
jlezard Avatar asked Apr 16 '12 09:04

jlezard


2 Answers

You could can also test the reverse condition:

sinceOp match {
  case Some(lastUpdate) if lastUpdate > update.time => //...
  case _ => //...
}

The second case covers both None and the case where the last update is smaller.

like image 162
paradigmatic Avatar answered Nov 12 '22 08:11

paradigmatic


Or you can replace pattern matching with chain of functions

sinceOp.filterNot(_ <= update.time).getOrElse(println("if None"))
like image 3
4e6 Avatar answered Nov 12 '22 09:11

4e6