2

I am trying to wrap a Java method that receives variable amount of parameters, example:

void info(String var1, Object... var2);

I used the following:

def info(message: String, any: Any*): Unit = {
   LOGGER.info(message, any)
}

But that doesn't work, it ends up calling an info that receives only 1 object:

void info(String var1, Object var2);

How can I solve this to call the java method that receives multiple parameters?

Thanks!

1

1 Answer 1

3

Try

def info(message: String, any: Any*): Unit = {
  LOGGER.info(message, any.asInstanceOf[Seq[Object]]: _*)
}

or

def info(message: String, any: AnyRef*): Unit = {
  LOGGER.info(message, any: _*)
}

without casting but not applicable to primitive types.

Sign up to request clarification or add additional context in comments.

2 Comments

The first works, but the second doesn't. Could it be that AnyRef doesn't have the primitives for Int? I found that here: docs.scala-lang.org/tour/unified-types.html
@FedeE. I wrote that the second wouldn't work for primitive types like Int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.