“扩展函数缺少参数类型”什么时候使用_(下划线)?
我在 scala 中经常遇到的一个问题是 lambda 表达式。例如,
JarBuilder.findContainingJar(clazz).foreach {userJars = userJars + _ }
给我一个错误,例如:
missing parameter type for expanded function ((x$1) => userJars.$plus(x$1))
然而,如果我自己进行扩展:
JarBuilder.findContainingJar(clazz).foreach {x => userJars = userJars + x }
它工作得很好。
这是 Scala 的错误吗?或者我做错了什么可怕的事情?
One problem I continually run into scala is with lambda' expressions. For instance
JarBuilder.findContainingJar(clazz).foreach {userJars = userJars + _ }
gives me an error like:
missing parameter type for expanded function ((x$1) => userJars.$plus(x$1))
Yet if I do the expansion myself:
JarBuilder.findContainingJar(clazz).foreach {x => userJars = userJars + x }
It works fine.
Is this a Scala bug? Or am I doing something horribly wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
匿名函数占位符语法的使用仅限于表达式。在您的代码中,您尝试在与表达式不同的赋值语句中使用通配符。
如果仔细查看错误,您可以看到赋值右侧的表达式正在扩展为匿名函数。
鉴于您想要实现的目标,您可能需要考虑以下事项
The usage of placeholder syntax for anonymous functions is restricted to expressions. In your code you are attempting to use the wildcard in an assignment statement which is not the same as an expression.
If you look closely at the error, you can see that the expression on the right hand side of your assignment is what is being expanded into an anonymous function.
Given what you are trying to accomplish however you may want to consider the following