How to assert some-ness


#1

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.


#2

Option#isDefined, Either#isRight, Either#isLeft


#3

Or Option#nonEmpty.


#4

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


#5

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.


#6

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


#7

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,
Patrick