Scala Optional doubt filter method

I was reading through the Scala docs, and looking through the Option

class I saw an implementation filter

that looks like this:

final def filter(p: A => Boolean): Option[A] = 
  if (isEmpty || p(this.get)) this else None

      

Why do we need to check if our instance is empty Option

? Wouldn't that lead to None

anyway?

+3


source to share


1 answer


||

is short-circuited, so checking isEmpty

first ensures that get

it will not be called on an empty option (to throw an exception).



+6


source







All Articles