: _* scala 中的符号 [英] : _* notation in scala

查看:46
本文介绍了: _* scala 中的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多 spark-scala 答案中遇到了这个:_* 符号,但找不到任何文档.这实际上是什么意思?这种用法的一个例子是这个问题的答案

I am encountering this : _* notation in many spark-scala answers, but couldn't find any documentation. What does it mean actually? An example of such usage is in the answer to this question

如何在 Spark 中使用带有 isin 的 DataFrame 过滤器爪哇?

行:

df.filter(col("something").isin(list: _*)

推荐答案

为了理解,举个例子

scala> def echo(args: String*) =
for (arg <- args) println(arg)
echo: (args: String*)Unit

scala>  val arr = Array("What's", "up", "doc?")
arr: Array[String] = Array(What's, up, doc?)

scala> echo(arr)
<console>:14: error: type mismatch;
 found   : Array[String]
 required: String
       echo(arr)
scala> echo(arr: _ *)
What's
up
doc?

这个符号,arr:_* 告诉编译器将 arr 的每个元素作为它自己的参数传递给 echo ,而不是将所有元素作为单个参数传递.

This notation,arr:_* tells the compiler to pass each element of arr as its own argument to echo , rather than all of it as a single argument.

这篇关于: _* scala 中的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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