Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala pattern match and logical not

I have:

 x match { 
  case a: SomeType => doSomething()
  case _ => doSomethingElse()
 }

Is there a way to check that 'a' is NOT of a given type?

like so:

x match { 
  case !(a: SomeType) => doSomething()
  case _ => doSomethingElse()
}

Edit: I agree the question may not be very clear but my interest was mostly the logical not and it has been answered.

like image 667
jakstack Avatar asked Jan 01 '23 23:01

jakstack


1 Answers

Something like this:

x match { 
  case a: if !a.isInstanceOf[MyFirst] => doSomething()
  case b: MySecond => doSomething()
  case _ => doSomethingElse()
}
like image 152
Pedro Correia Luís Avatar answered Jan 09 '23 18:01

Pedro Correia Luís