Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does :_* convert ordered collections into variable arg lists?

I have been using :_* to convert Seq[String] to String* and I realized that I do not understand how this works under the hood.

Is there a simple way to think about this?

like image 376
Jesse Avatar asked Feb 28 '12 19:02

Jesse


People also ask

How do you pass a list to a variable argument in Java?

Use List. toArray(T[] arr) : yourVarargMethod(yourList.

How do you pass variable arguments in Python?

**kwargs allows us to pass a variable number of keyword arguments to a Python function. In the function, we use the double-asterisk ( ** ) before the parameter name to denote this type of argument.

What datatype are the * args stored when passed into a function?

As what datatype are the *args stored, when passed into a function? List.

Do you need to declare an out variable before you use it in C#?

Variables passed as out arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns.


1 Answers

Under the hood, String* is passed as a Seq[String]. It's all just syntactic sugar:

def blah(ss: String*) = {...}
blah("Hi","there")

is turned into

def blah(ss: Seq[String]) = {...}
blah(Seq("Hi", "there"))

and :_* just means "hold the sugar, I've already got what you need--a Seq!"

like image 146
Rex Kerr Avatar answered Oct 30 '22 04:10

Rex Kerr