missing parameter type for simple expression

2019-07-21 14:42发布

following the play websockets example i run into a weird problem.

the following example from the docs is working:

  Future.successful(request.session.get("user") match {
    case None => Left(Forbidden)
    case Some(_) => Right(out => ChannelActor.props(out, "", ""))
  })

to understand the code I tried to play around with the code:

  Future.successful({
    val test = request.session.get("user") match {
      case None => Left(Forbidden)
      case Some(_) => Right(out => ChannelActor.props(out, "", ""))
    }
    test
  })

the compiler complains, that there is a "missing parameter type" for the value out.

Why is that? I just added the saved the Either in test and returned this val instead of the match statement itself.

thanks

1条回答
走好不送
2楼-- · 2019-07-21 15:21

The reason is that the Scala compiler cannot infer the type of out. You never tell the compiler (or reader) what type out actually is. You can see that it is a functions returns type Props, but what type if its input is is never specified. Therefore, the type information you can derive from this code is just Future[Either[Result, ??? => Props].

You solved this by annotating this handler function val handler: HandlerProps which is an alias for val handler: ActorRef => Props. So by annotating it with type information, you supplied the Scala compiler with enough type information to compile this. You can also do this by writing Right(out: ActorRef => ChannelActor.props(out, "", "")), or writing val test: Either[Result, HandlerProps] = ... match {.

查看更多
登录 后发表回答