匿名函数的参数类型必须是完全已知的. (SLS 8.5) [英] The argument types of an anonymous function must be fully known. (SLS 8.5)
问题描述
我有一个函数文字
{case QualifiedType(preds, ty) =>
t.ty = ty ;
Some((emptyEqualityConstraintSet,preds)) }
这会导致错误消息
missing parameter type for expanded function The argument types of an anonymous function
must be fully known. (SLS 8.5) Expected type was:
? => Option[(Typer.this.EqualityConstraintSet, Typer.this.TypeRelationSet)]
I looked in SLS 8.5, but didn't find an explanation.
如果我将功能扩展到
{(qt : QualifiedType) =>
qt match {case QualifiedType(preds, ty) =>
t.ty = ty ;
Some((emptyEqualityConstraintSet,preds)) }}
错误消失了.
(a)为什么会出错?
(b)我该怎么解决?
我尝试了一个明显的解决方法,即在模式和=>之间添加: QualifiedType
,但这是一个语法错误.
I tried the obvious fix, which was to add : QualifiedType
between the pattern and the =>, but this is a syntax error.
我注意到的一件事是上下文会有所作为.如果我使用函数文字作为声明为期望QualifiedType => B
的函数的参数,则没有错误.但是,如果我将其用作期望A => B
的函数的参数,则会出现错误.我希望这里发生的事情是,因为可以想象将模式应用于类型为QualifiedType的超类型的对象,所以编译器在不保证不会将该函数应用于以下对象的情况下不愿意分配明显的类型.任何不是QualifiedType的东西.我真正想要的是能够写{QualifiedType( preds, ty) => ...}
并具有与Haskell的\QualifiedType(preds,ty) -> ...
相同的含义.
One thing I noticed is that the context makes a difference. If I use the function literal as an argument to a function declared as expecting a QualifiedType => B
, there is no error. But if I use it as an argument to a function expecting an A => B
, there is an error. I expect that what is going on here is that, as the pattern could conceivably be applied to an object whose type is a supertype of QualifiedType, the compiler is not willing to assign the obvious type without assurance that the function won't be applied to anything that isn't a QualifiedType. Really what I'd like is to be able to write {QualifiedType( preds, ty) => ...}
and have it mean the same thing as Haskell's \QualifiedType(preds,ty) -> ...
.
推荐答案
Here's the SLS quote, for the rest of us:
必须部分定义此类表达式的预期类型.它 对于k> 0,必须为
scala.Functionk[S1, . . . , Sk, R]
,或者scala.PartialFunction[S1, R]
,其中参数类型为S1,.... . . , 必须完全确定Sk,但结果类型R可以是 未定.
The expected type of such an expression must in part be defined. It must be either
scala.Functionk[S1, . . . , Sk, R]
for some k > 0, orscala.PartialFunction[S1, R]
, where the argument type(s) S1, . . . , Sk must be fully determined, but the result type R may be undetermined.
否则,您回答了问题.
这篇关于匿名函数的参数类型必须是完全已知的. (SLS 8.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!