缺少用于简单表达式的参数类型 [英] missing parameter type for simple expression
问题描述
按照播放网络套接字示例,我遇到了一个奇怪的问题.>
文档中的以下示例正在运行:
Future.successful(request.session.get("user") match {
case None => Left(Forbidden)
case Some(_) => Right(out => ChannelActor.props(out, "", ""))
})
了解我尝试使用的代码:
Future.successful({
val test = request.session.get("user") match {
case None => Left(Forbidden)
case Some(_) => Right(out => ChannelActor.props(out, "", ""))
}
test
})
编译器抱怨说,值out
存在缺失的参数类型".
那是为什么?我刚刚添加了保存的Either in test,并返回了此val而不是match语句本身.
谢谢
原因是Scala编译器无法推断out
的类型.您永远不会告诉编译器(或阅读器)out
的实际类型.您可以看到它是一个返回类型为Props
的函数,但从未指定输入该类型的类型.因此,您可以从此代码派生的类型信息仅为Future[Either[Result, ??? => Props]
.
您通过注释此处理函数val handler: HandlerProps
(是val handler: ActorRef => Props
的别名)解决了此问题.因此,通过使用类型信息对其进行注释,可以为Scala编译器提供足够的类型信息以对其进行编译.您也可以通过编写Right(out: ActorRef => ChannelActor.props(out, "", ""))
或编写val test: Either[Result, HandlerProps] = ... match {
来做到这一点.
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
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 {
.
这篇关于缺少用于简单表达式的参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!