Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala String* type (in function args)

Tags:

syntax

scala

I have the following method:

def m(a: String*) = { // ... }

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!

like image 650
hummingBird Avatar asked Aug 09 '18 10:08

hummingBird


Video Answer


1 Answers

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
like image 95
pamu Avatar answered Oct 04 '22 11:10

pamu