为什么Spark数据集的foreach方法需要显式的函数转换,而map不需要?

时间:2018-11-09 01:13:51

标签: scala apache-spark

UserData.retrieve类型为(m: Map[String, String]) => User的地方 并且UserData.update的类型为(r: spark.sql.Row) => Unit

为什么

receipts map UserData.retrieve

工作正常,但是当我跑步

events foreach UserData.update

它导致以下错误:

missing argument list for method update in object UserData Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing `update _` or `update(_)` instead of `update`.

我在文档中看到map期望func: (T) ⇒ Uforeach期望f: (T) ⇒ Unit,所以在我看来,编译器应该将{{ 1}}和map,但仅对foreach这样做。为什么?

n.b。我知道我可以通过显式转换来使此map通话成功

foreach

但是在这里我要寻求帮助,以了解我为什么要这么做。

1 个答案:

答案 0 :(得分:1)

只有在需要函数类型时,才会将未应用的方法转换为函数。

数据集上的foreach重载,可以用ForeachFunction [T]或T => Unit调用。

编译器没有足够的信息来解决正在调用哪个重载方法,因此将不应用该函数。