简体   繁体   中英

What is :_* and How it implement in Scala?

What i know:

scala> def fx(s: String *) = s.foreach(println)
fx: (s: String*)Unit

scala> val lst = List("1","2","3")
lst: List[java.lang.String] = List(1, 2, 3)

scala> fx(lst:_*)
1
2
3

What i want to know:

  1. How can I implement :_* ? by map?
  2. Is there any other way that replace it?
  3. How :_* defined in Scala?

Thank you

It is only a syntactic sugar to indicates to the compiler that you already provide a sequence of elements, there is no other "implementation" of it. For more information, you can refer to the Scala Language Specification (§6.6, p. 78)

For a method that takes variable arguments :_* means you want to pass the members of a collection as the variable arguments.

The corresponding varargs example to yours above:

scala> fx("a", "b", "c")
a
b
c

You can't implement -- it is a language feature. It doesn't mean anything outside the context of calling a varargs method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM