17.5. Annotating varargs Methods
Problem
You’ve created a Scala method with a varargs field, and would like to be able to call that method from Java code.
Solution
When a Scala method has a field that takes a variable number of
arguments, mark it with the @varargs
annotation.
For example, the printAll
method in the following Scala class is marked with @varargs so it can be called as desired from
Java:
packagevarargsimportscala.annotation.varargsclassPrinter{@varargsdefprintAll(args:String*){args.foreach()println}}
The printAll method can now be
called from a Java program with a variable number of parameters, as
shown in this example:
packagevarargs;publicclassMain{publicstaticvoidmain(String[]args){Printerp=newPrinter();p.printAll("Hello");p.printAll("Hello, ","world");}}
When this code is run, it results in the following output:
Hello Hello, world
Discussion
If the @varargs annotation
isn’t used on the printAll method,
the Java code shown won’t even compile, failing with the following
compiler errors:
Main.java:7: printAll(scala.collection.Seq<java.lang.String>) in
varargs.Printer cannot be applied to (java.lang.String)
[error] p.printAll("Hello");
[error] ^
Main.java:8: printAll(scala.collection.Seq<java.lang.String>) in
varargs.Printer cannot be applied to (java.lang.String,java.lang.String)
[error] p.printAll("Hello, ", "world");
[error] ^Without the @varargs
annotation, from a Java perspective, the printAll method appears to take a scala.collection.Seq<java.lang.String> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access