joesan
joesan

Reputation: 15385

Scala Function Returning Object Type

I have a definition that looks like this:

sealed trait ServiceResponse
  case object OK extends ServiceResponse
  case object SHIT extends ServiceResponse

  private def remoteService(waitTime: FiniteDuration): ServiceResponse = {
    val awaitable: Future[Ok.type] = Future.successful {
      Thread.sleep(waitTime.toMillis)
      OK
    }
    Await.ready(awaitable, waitTime)
  }

The remoteService function complaints saying that it is expecting a type. So why this this a problem? Why can't I simply return a Object type?

Upvotes: 0

Views: 535

Answers (1)

Boris
Boris

Reputation: 611

It's because you need to use Await.result instead of Await.ready. This works:

import scala.concurrent._
import scala.concurrent.duration._

sealed trait ServiceResponse {

  case object OK extends ServiceResponse
  case object SHIT extends ServiceResponse

  private def remoteService(waitTime: FiniteDuration): ServiceResponse = {
    val awaitable: Future[OK.type] = Future.successful {
      Thread.sleep(waitTime.toMillis)
      OK
    }
    Await.result(awaitable, waitTime)
  }

}

Also you've got a typo: Future[Ok.Type] should be Future[OK.type]

Upvotes: 1

Related Questions