jlezard
jlezard

Reputation: 1437

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

Upvotes: 5

Views: 235

Answers (2)

paradigmatic
paradigmatic

Reputation: 40461

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.

Upvotes: 10

4e6
4e6

Reputation: 10776

Or you can replace pattern matching with chain of functions

sinceOp.filterNot(_ <= update.time).getOrElse(println("if None"))

Upvotes: 3

Related Questions