I am trying to read input in scalatra webapp using for iteration in below manner
for (
input1<- params.getOrElse("input1", "");
input2 <- params.getOrElse("input2", "");
search <- params.getOrElse("search", false)
) {
//Perform action
}
It is throwing error value foreach is not a member of Object.
In case if user doesn't provide any input, it should default empty string and call the action instead of breaking out of loop. For that reason i am using getOrElse. But i haven't succeeded.
Any help?
You can use for-comprehensions to work with
Optionbut you can't use it for objects that don't have at least one definedforeach,maporflatMap. In your case, ifparamsis returning options then...Wherein this will not run if both
inputandinput2are notNone.If you don't want to short circuit the control logic and you want there to be sane defaults for
Optionreturns, then a better way is just to dowhere you're explicitly providing the defaults and passing them into a function call.