I am implementing a Java interface containing variadic methods like so:
interface Footastic {
void foo(Foo... args);
}
Is it possible to implement this interface in Scala? Variadic functions are handled differently in Scala, so the following won’t work:
class Awesome extends Footastic {
def foo(args: Foo*): Unit = { println("WIN"); }
// also no good: def foo(args: Array[Foo]): Unit = ...
}
Is this even possible?
The code you’ve written works as-is.
The scala compiler will generate a bridge method which implements the signature as seen from Java and forwards to the Scala implementation.
Here’s the result of running javap -c on your class Awesome exactly as you wrote it,
The first foo method with with Seq<Foo> argument corresponds to the Scala varargs method in Awesome. The second foo method with the Foo[] argument is the bridge method supplied by the Scala compiler.