-
Notifications
You must be signed in to change notification settings - Fork 21
Scala varargs implementation's use of Seq impairs Java interop #1342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Imported From: https://issues.scala-lang.org/browse/SI-1342?orig=1 |
Bruno Bieth (mustaghattack) said: I have a java interface defined as follow : interface JavaVarArgsInterface {
void varArgsMethod( String ... args );
} I want to implement it in scala now : class ScalaVarArgsImpl extends JavaVarArgsInterface {
def varArgsMethod( args : String*) {
for( arg <- args ) println( arg )
}
} The compilation will pass but will generate the following class : public class ScalaVarArgsImpl extends java.lang.Object implements JavaVarArgsInterface,scala.ScalaObject{
public ScalaVarArgsImpl();
public void varArgsMethod(scala.Seq);
public int $$tag() throws java.rmi.RemoteException;
} Which obviously doesn't implement properly the JavaArgsInterface. With 2.7.1-final we would do the following : class ScalaVarArgsImpl extends JavaVarArgsInterface {
def varArgsMethod( args : Array[String]) {
for( arg <- args ) println( arg )
}
} And it would compile as expected. If you compile this with 2.7.2-final you'll get : ScalaVarArgsImpl.scala:1: error: class ScalaVarArgsImpl needs to be abstract, since method varArgsMethod in trait JavaVarArgsInterface of type (java.lang.String*)Unit is not defined
class ScalaVarArgsImpl extends JavaVarArgsInterface {
^
one error found |
Stefan Endrullis (xylo) said: |
Ralph Apel (rapel) said: I coded the test case provided by mustaghattack. Then I added the following printout to the nonPrivateMembers cycle in
println("member="+member) Here are the different printouts I obtained for 2.7.6 and 2.8.0 2.7.6: 2.8.0: So, there are two relevant differences:
For me this issue popped up while trying to adapt netgents scala-scripting solution to 2.8.0. |
Ralph Apel (rapel) said: Just had a better look at the printout for 2.8.0: varArgsMethod appears two times!! 1: member=method varArgsMethod member.isDeferred=false !(clazz hasFlag ABSTRACT)=true !isAbstractTypeWithoutFBound(member)=true (member hasFlag JAVA)=false (javaErasedOverridingSym(member) != NoSymbol?)=false !((member hasFlag JAVA) && (javaErasedOverridingSym(member) != NoSymbol?))=true 2: member=method varArgsMethod member.isDeferred=true !(clazz hasFlag ABSTRACT)=true !isAbstractTypeWithoutFBound(member)=true (member hasFlag JAVA)=true (javaErasedOverridingSym(member) != NoSymbol?)=false !((member hasFlag JAVA) && (javaErasedOverridingSym(member) != NoSymbol?))=true (1) is coincident with the output from 2.7.6 and it seems to represent the method from ScalaVarArgsImpl?.scala (2) seems to describe the abstract method from JavaVarArgsInterface?.java How does the latter get into clazz.tpe.nonPrivateMembers ? |
Ralph Apel (rapel) said: |
@odersky said: |
Ralph Apel (rapel) said: |
@odersky said: |
@odersky said: |
Stefan Endrullis (xylo) said: I tried it already and it worked fine in my first test case. But there's still an open issue. Consider the following classes:
public interface ScalaSeqInterface {
public String f(String... seq);
}
class ScalaSeq extends ScalaSeqInterface {
def f(seq: String*): String = "asdf"
}
public class ScalaSeqTest {
public static void main(String[] args) {
System.out.println(((ScalaSeqInterface) new ScalaSeq()).f("")); // compiles
System.out.println(new ScalaSeq().f("")); // does not compile
}
} Why can't I use PS: But since there's a workaround (via casting) for this problem, there might be more urgent issues at the moment. |
@SethTisue said: |
@odersky said: It's because Scala and Java use different representations for varargs. For Java it's an array whereas for Scala it's a Seq (or, more, precisely, a WrappedArray, which is a subclass of Seq.) |
(No description for SI-1342.)
The text was updated successfully, but these errors were encountered: