I have a function called or
for example, which is defined as;
or(filters: FilterDefinition*)
And then I have a list:
List(X, Y, Z)
What I now need to do is call or
like
or(func(X), func(Y), func(Z))
And as expected the length of the list may change.
What's the best way to do this in Scala?
In Python, you can unpack list , tuple , dict (dictionary) and pass its elements to function as arguments by adding * to list or tuple and ** to dictionary when calling function.
yes, using *arg passing args to a function will make python unpack the values in arg and pass it to the function.
In Python, we can use *args to pass a variable number of arguments to a function. Now, since a list has multiple values, people tend to use *args as the parameter variable for a list argument so that all the values of the list are taken care of.
Take a look at this example, I will define a function printme that takes vargs of type String
def printme(s: String*) = s.foreach(println) scala> printme(List("a","b","c")) <console>:9: error: type mismatch; found : List[String] required: String printme(List(a,b,c))
What you really need to un-pack the list into arguments with the :_*
operator
scala> val mylist = List("1","2","3") scala> printme(mylist:_*) 1 2 3
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