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?
Use List. toArray(T[] arr) : yourVarargMethod(yourList.
**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.
As what datatype are the *args stored, when passed into a function? List.
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.
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!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With