Will_Panda
Will_Panda

Reputation: 534

Passing an "Any*" varargs argument to another function that takes "Any*"

I have a function like below which is auto generated by a script.

def printFunc(args : Any*) : Unit = {
     funcCallHandler.call(args)
}

The call function in the funcCallHandler looks like this. This function takes variable arguments from other generated functions too.

def call(args : Any*) : Unit = {

   for (arg <- args) {
       /*Check type using a match function
        and add do the rest of the code*/
   }

}

When I am passing the variable arguments I got from the first function to the second mentioned one I get it as a WrappedArray. Is there a way to type match this WrappedArray or is there a better approach at doing this?

The type is not specified in the case of first function.

Upvotes: 0

Views: 454

Answers (1)

Cyrille Corpet
Cyrille Corpet

Reputation: 5315

To pass the arguments as varargs, you need to do

 def printFunc(args : Any*) : Unit = {
   funcCallHandler.call(args: _*)
}

Otherwise, args will be understood as a single argument of type Seq[Any].

Upvotes: 4

Related Questions