Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm wondering what the use is of the asterisk (*) symbol in this syntax? I'm obviously new to Scala. I googled but am probably googling the wrong thing. Any help is appreciated.

Cheers!

I wouldn't call it a duplicate of this question. It would be misleading to do so, because your link describes a situation when someone knows it's a var-len arg list, which I didn't know at the moment. hummingBird Aug 9, 2018 at 10:24

Its called as "var args" (variable arguments).

def concat(strs: String*): String = strs.foldLeft("")(_ ++ _)

Scala REPL

scala> def concat(strs: String*): String = strs.foldLeft("")(_ ++ _)
concat: (strs: String*)String
scala> concat()
res6: String = ""
scala> concat("foo")
res7: String = foo
scala> concat("foo", " ", "bar")
res8: String = foo bar
                Thanks, that does it. I'm very much aware of the concept, but unfortunately I read just one tutorial for scala and it's been some months ago. If I revisited it, it would have been enough. Unfortunately, I went by stupid assumption it's a type :$
– hummingBird
                Aug 9, 2018 at 10:21
                @hummingBird You can easily see whether it's a type constructor by trying it out in isolation: type A = (Int*) doesn't compile. By the way: type A = (=> Int) also doesn't compile. So, those things are not type constructors, they are rather modifiers that determine how methods receive their arguments. And, if it solves your problem, could you please accept the answer then?
– Andrey Tyukin
                Aug 9, 2018 at 10:42
                @AndreyTyukin thanks for the explanation. I actually clicked several times, but one cannot accept an answer in <10min. Then I went for a break and now that I'm here - BOOM :).
– hummingBird
                Aug 9, 2018 at 11:13

This is called a repeated parameter (see Section 4.6.3 of the Scala Language Specification).

Repeated Parameters allow a method to take an unspecified number of arguments of the same type T, which are accessible inside the method body bound to a parameter of type Seq[T].

In your case, inside the method m, the parameter a will be bound to a Seq[String].

That's the syntax to define a method that take a variable number of arguments.

Your m method can take 0, 1 or more arguments and these are all valid invocations:

m("hello") m("hello", "world")

You can also pass a collection to that method if you use the appropriate type hint:

val words = Seq("hello", "world")
m(words: _*)

You can play around with this code here on Scastie (where I implemented m as the concatenation of the input strings).

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.