Reputation: 30825
Pretty new to scala. I have this function:
def retrieveValue(valueName:String) : Double = {
for (ln <- io.Source.stdin.getLines) {
val parseResult = parseValue(ln)
parseResult match {
case Right(x) => return x
case Left(error) => println(error.message + " Please try again.")
}
}
}
And I'm getting this compile error:
QuadSolver.scala:14: error: type mismatch;
found : Unit
required: Double
for (ln <- io.Source.stdin.getLines) {
What exactly am I doing wrong?
Upvotes: 1
Views: 46
Reputation: 22171
parseResult match {
case Right(x) => x //note that `return` keyword is not needed
case Left(error) => println(error.message + " Please try again.") //returns Unit
}
This piece of code return either a Double
or a Unit
(Unit
provided by println
), therefore compiler expecting a Double
as method's return type obviously complains.
In functional programming, it's better to have each function strictly following Single-Responsibility Principle.
Thus, you should have one function aiming to retrieve the value and one to print the result.
Upvotes: 1