How to assert some-ness

I have a value which is either Some(x) or None. What is the correct way to use assert to assert that it is Some rather than None. Do I have to use assert(value != None)? I can’t think of an easy way to assert that it is Some(x) for some x.

In the case of Option I can assert that it is not None, but in the case of Either, I might light to assert that it is Right or assert that it is Left.

Option#isDefined, Either#isRight, Either#isLeft

1 Like

Or Option#nonEmpty.

1 Like

What is the #? Is that part of the name or is that an operator?

I think it’s a notation that stems from Java. It just says method nonEmpty in class Option. It’s kind of analogous to type projections (Class#Type) in Scala, only it’s not actually valid syntax for methods.

(I think it’s helpful for distinguishing that it’s a method on the class and not on the companion object.)

1 Like

In addition to the other answers, if you need this in the context of ScalaTest, you can use the built-in matchers and more:

Best regards,