score:24
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]
score:14
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.
Source: stackoverflow.com
Related Query
- Check for None in Scala Option type isEmpty method
- How to check if a scala Option type is None in Java code
- Using Scala reflection to check for a method on an object or to find key type on a Map
- How to access a Java static method from Scala given a type alias for that class it resides in
- Why does Scala implicit resolution fail for overloaded method with type parameter?
- Scala value class compilation fails for base type with partial-function-parameter method
- What is the best way to assert for a scala method whose return type is Future[Unit]
- check empty for option string in scala
- Type overriding for parametrized method in scala
- Method to check reference equality for Any type
- Scala method overload for a special case of a generic type
- Scala compiler is complaining about type mismatch for generic parameter on method level
- Intellij hint for output type of a scala method
- reason for scala apply method type error(variadic function)
- Scala type classes for transform on option using higher order functions
- Return type for this scala method : String vs Any
- Constraint a scala type such that I would never need to check for null?
- Scala check at runtime whether an object has a the same type as a given parameterized type of a method invocation
- Scala Option something like flatMap but for None case?
- scala design suggestion needed with factory method for type classes
- Scala compiler unhappy due to type issues (believe it is due to Option and List) in a for comprehension
- Scala return Object type for Map method
- Scala flatMap method on Option type cannot be resolved. What is the correct way to do this?
- getOrElse for an Option(null) does not return a None type or the default value when null in Scala
- Scala underscore - ERROR: missing parameter type for expanded function
- Scala classOf for type parameter
- Scala generic method - No ClassTag available for T
- How the get the classOf for a scala object type
- Should I use Unit or leave out the return type for my scala method?
- What is the rule for parenthesis in Scala method invocation?
More Query from same tag
- Scala underscore use to simplify syntax of function literals
- Akka + scala reverse server error
- Sbt generated docker container fails to package subproject
- Scala -- are map and filter operations in linear time?
- How to convert nested sequences to a sequence of tuples?
- How to run main in a Scala object from Maven
- How much trust should I put in the validity of retrieved data from database?
- Exception serializing Enumeration field to BSON
- How to get corresponding quarter of previous year in Scala
- How is the `type` keyword implemented in Scala
- scala validate password and confirm password in play framework
- Play Framework Ning WS API encoding issue with HTML pages
- Spark writing multiple files
- Akka actor - messaging neighbours
- Resource directory for tests in a Play application
- Parsing recursive JSON structure with Lists in Scala
- Cannot access the parameter of a Menu.param from a Lift Snippet
- using binary files as a dependency
- How to write a cache loader for Caffeine LoadingCache in "Scala" for "refreshAfterWrite" to work
- Scala - how to define a structural type that refers to itself?
- Play Framework: How to serialize raw fields to JSON with nested elements
- Mapping many Eithers to one Either with many
- How to remove List() and escape characters from response JSON in Scala
- Unexpected behavior when loading SnowFlake data from Databricks environment
- A Monoid application to subtypes doesn't compile with append operator, but works when explicitly called
- How to implement start, stop and error methods in play framework(2.5.x)
- how to modify self-defined scala operators' precedence
- Scala Spark job to upload CSV file data to mongo DB fails due to CodecConfigurationException
- Can `extends` be followed by no values?
- Scalacheck - Add parameters to commands