匿名函数的参数类型必须是完全已知的.(SLS 8.5) [英] The argument types of an anonymous function must be fully known. (SLS 8.5)

查看:19
本文介绍了匿名函数的参数类型必须是完全已知的.(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)]

我查看了 SLS 8.5,但没有找到解释.

I looked in SLS 8.5, but didn't find an explanation.

如果我自己将函数扩展为

If I expand the function myself to

{(qt : QualifiedType) =>
  qt match {case QualifiedType(preds, ty) =>
               t.ty = ty ;
               Some((emptyEqualityConstraintSet,preds)) }}

错误消失了.

(a) 为什么这是一个错误?

(a) Why is this an error?

(b) 我能做些什么来修复它?

(b) What can I do to fix it?

我尝试了明显的修复方法,即在模式和 => 之间添加 : 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) -> ....

推荐答案

这里是 SLS 引用,对于我们其他人:

Here's the SLS quote, for the rest of us:

必须部分定义此类表达式的预期类型.它必须是 scala.Functionk[S1, ..., Sk, R] 对于某些 k > 0,或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, or scala.PartialFunction[S1, R], where the argument type(s) S1, . . . , Sk must be fully determined, but the result type R may be undetermined.

否则,您已经回答了您的问题.

Otherwise, you answered your question.

这篇关于匿名函数的参数类型必须是完全已知的.(SLS 8.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆