Thursday, 12 September 2013

Scala, how to use Some(value) from an Option implicitly?

Scala, how to use Some(value) from an Option implicitly?

I have a piece of code that extracts a string from a request body, but it
may not be there, so it is an Option[String]. If there is a value, then I
wish to use it implicitly.
To do this conversion, I write implicit val code = googleCode.
Is there a way to make googleCode an implicit String so that I can use it
directly rather than creating an implicit val with the value of
googleCode?
request.getQueryString("code") match {
case None =>
Logger.error("unable to retrieve authentication code from google
request")
Redirect(routes.Application.index())
case Some(googleCode) => Async {
implicit val code: String = googleCode // <== CONVERTING TO AN
IMPLICIT
Logger.debug("retrieved authentication code, proceeding to get
token")
...
Ok("congratulations, ${user.name}, you are logged in!")
Note, the code snippet is from a Playframework Controller, but this is
more a question of the Scala language in general

No comments:

Post a Comment