1
votes

I am new to scala and I have no idea at all what's wrong with my code:

I have some Future which may throw exception:

var clean: Future[Boolean];

However, when I try to map

clean.map( b => {
  case true => Ok("success")
  case false => Ok("failed")
}).recover {
  case t => Ok("error: " + t)
}

I get compile error specified in the title.. I totally have no idea where and/which part of my code exactly causes it.. I have googled but it is so difficult to find any clue because scala syntax is so flexible thus so many variations in the internet but the error I get is quite misleading...

Any help will be very appreciated...

2

2 Answers

4
votes

The function you are passing to .map as written is supposed to accept a boolean argument b, and return another function (the stuff in curly braces). The compiler cannot guess the type of that inner function, so it complains about the parameter type not being known.

That's actually a good thing, because it's not what you really wanted to write at all.

Just remove the whole (b => ...) business:

clean.map { 
  case true => Ok("success")
  case false => Ok("failed") 
}.recover { 
  case t => Ok("error: " + t)
}

Also, make clean val rather than var. Mutable variables aren't a good idea. 99% of time, writing code in scala, you should not need them, so, I'd recommend that you just pretend that var keyword doesn't exist at all for now, until you learn the language well enough to be able to identify that 1% of cases where mutable state is actually needed.

2
votes

I guess you intended:

clean.map( b => b match {
 case true => Ok("success")
 case false => Ok("failed")
}).recover {
 case t => Ok("error: " + t)
}