映射 Map 时 Scala 不匹配
我正在使用 Scala (2.9.1) 迈出第一个有趣的步骤(非 hello-world 级别),但我一直在试图理解一条非常无信息的错误消息。 它很像这样:
error: type mismatch;
found : (Int, Array[InputEntry]) => (Int, Double)
required: (Int, Array[InputEntry]) => ?
entries.groupBy(grouper).map((k: Int, ies: Array[InputEntry]) => (k, doMyStuff(ies)))
正如您所猜测的,此代码片段中的进程应该是进行某些处理的地方,它实际上是一个定义良好的函数,其签名为 Array[InputEntry] =>;双倍
。
相反,Grouper 的签名是 Array[InputEntry] =>整数。
我尝试提取一个函数并替换 lambda,但它没有用,而且我一直试图理解错误中的问号......
有什么想法吗?
编辑:我应该澄清一下,InputEntry是我定义的一个类,但就这个例子而言,在我看来它几乎不相关。
I'm taking my first interesting steps (non-hello-world level) with Scala (2.9.1) and I'm stuck trying to understand a very uninformative error message.
It goes much like this:
error: type mismatch;
found : (Int, Array[InputEntry]) => (Int, Double)
required: (Int, Array[InputEntry]) => ?
entries.groupBy(grouper).map((k: Int, ies: Array[InputEntry]) => (k, doMyStuff(ies)))
As you can guess process in this snippet is supposed to be where some processing goes on, and it's actually a well defined function with signature Array[InputEntry] => Double
.
Grouper's signature, instead, is Array[InputEntry] => Int
.
I've tried to extract a function and replace the lambda but it was useless, and I'm stuck trying to understand the question mark in the error...
Any ideas?
Edit: I should clarify that InputEntry is a class I've defined, but for the sake of this example it seems to me like it's hardly relevant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来像问题:
您需要使用 case 语句来取消应用参数并将它们分配给局部变量。您还需要使用 {} 而不是 (),因为它现在是匿名函数。
这是一个更简单的例子。
如果您不想使用 case 语句,则必须将元组保留为单个变量。
This looks like the problem:
You need to use a case statement to unapply the params and assign them to local variables. You also need to use {} instead of () because it is an anonymous function now.
Here's a more simple example.
If you don't want to use a case statement, you have to keep the tuple as a single variable.