Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for None in Scala Option type isEmpty method

Tags:

I'm using the Option Type's isEmpty method to check if there is no value. I do not want to use the case match as in my situation, I just want to check if there is None as I would throw an error to the caller. But the isEmpty method fails even though the value is None.

Here is what I tried!

val questionOption = Question.getQuestionForQuestionId(userExam.get.examId, currQuesId + 1)  if(questionOption.isEmpty) {     Left(Failure(FailureCode.NO_DATA_FOUND, "Cannot get next exam question you tampered your cookie or cookie is lost.... >> TODO... modify the exception message")) }  

It is not getting inside the if condition. I tried to do a println on the questionOption and it prints None. So wondering why I'm not getting inside the if condition.

like image 637
joesan Avatar asked Dec 30 '13 16:12

joesan


People also ask

What are option some and none in Scala?

An Option[T] can be either Some[T] or None object, which represents a missing value. For instance, the get method of Scala's Map produces Some(value) if a value corresponding to a given key has been found, or None if the given key is not defined in the Map.

What is isDefined in Scala?

Definition Classes IterableOps.  final def isDefined: Boolean. Returns true if the option is an instance of scala. Some, false otherwise. Returns true if the option is an instance of scala.Some, false otherwise.

What is some () in Scala?

Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some. We can use this with the collection.

How does Option work in Scala?

The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.


2 Answers

From the comment under the question, the real problem emerges:

 val questionOption = Question.getQuestionForQuestionId(userExam.get.examId, currQuesId + 1)   if(questionOption.isEmpty) {     Left(Failure(FailureCode.NO_DATA_FOUND, "Cannot get next exam question you tampered your cookie or cookie is lost.... >> TODO... modify the exception message"))   } 

By itself, if returns type Unit so that your statement is returning nothing useful. If you want to return something you need to add in either an else which then returns the least upper bound of the result types. Hence

 >>> val yo = if(1 != 0) 4  yo: Unit   >>> val ya = if(1 != 0) Left(1) else Right("got it")  ya: Either[Int, String] 
like image 52
wheaties Avatar answered Oct 12 '22 02:10

wheaties


You could just do a boolean check to see of the value is None and throw the error to the caller if it is, otherwise continue processing:

scala> val o: Option[Any] = None o: Option[Any] = None  scala> println(o == None) true  scala> println(o != None) false 

But maybe a better way to accomplish what you're trying to do, alert the caller of the error or continue processing, would be to use Scala's Try idiom to handle errors.

like image 35
axiopisty Avatar answered Oct 12 '22 03:10

axiopisty