[英]scala syntax understanding _* and type*
I am having some difficulty in understanding this syntax: 我在理解此语法时遇到一些困难:
(as: List[A]) => val h = insert(e, as: _*)}
and 和
def insert(h: H, as: A*): H = as.foldLeft(h)((hh, a) => insert(a, hh))
What do _*
and A*
mean? _*
和A*
是什么意思?
Thanks. 谢谢。
A*
is an argument define as a vararg, it's equivalent to A...
in Java. A*
是一个定义为vararg的参数,它等效于Java中的A...
Exemple : 范例:
scala> def f(i: Int*) = i.length
f: (i: Int*)Int
scala> f(1,2,3)
res50: Int = 3
:_*
is a transformer that allow to transform a param of type List into a vararg. :_*
是一个转换器,可以将List类型的参数转换为vararg。
Exemple : 范例:
scala> f(List(1,2,3):_*)
res51: Int = 3
def insert(h: H, as: A*): H = as.foldLeft(h)((hh, a) => insert(a, hh))
A* represents a vararg : you can supply as many As as you wish to the method A *代表可变参数:您可以根据需要提供任意数量的方法
(as: List[A]) => val h = insert(e, as: _*)}
in this case a Sequences is converted to a vararg parameter ( a single list is converted into n single arguments having the type of A). 在这种情况下,序列将转换为vararg参数(单个列表将转换为n个类型为A的单个参数)。
sometimes this is necessary, imho it doesnt change too much on the conceptual level (as you still can invoke fold , map etc on both) 有时这是必要的,恕我直言,它在概念层面上并不会改变太多(因为您仍然可以同时调用fold,map等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.