Scala 下划线 - 错误:缺少扩展函数的参数类型 [英] Scala underscore - ERROR: missing parameter type for expanded function

查看:53
本文介绍了Scala 下划线 - 错误:缺少扩展函数的参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对此有很多问题,但我创建了一个我认为应该可行的简单示例,但仍然没有,我不确定我明白为什么

I know there have been quite a few questions on this, but I've created a simple example that I thought should work,but still does not and I'm not sure I understand why

val myStrings = new Array[String](3)
// do some string initialization

// this works
myStrings.foreach(println(_))


// ERROR: missing parameter type for expanded function
myStrings.foreach(println(_.toString))

谁能解释为什么第二个语句不能编译?

Can someone explain why the second statement does not compile?

推荐答案

它扩展为:

myStrings.foreach(println(x => x.toString))

你想要:

myStrings.foreach(x => println(x.toString))

匿名函数的占位符语法用函数替换尽可能小的包含表达式.

The placeholder syntax for anonymous functions replaces the smallest possible containing expression with a function.

这篇关于Scala 下划线 - 错误:缺少扩展函数的参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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