Skip to content
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

Error when implementing java varargs method on anonymous type with explicit type #4729

Closed
scabug opened this issue Jun 22, 2011 · 5 comments
Closed

Comments

@scabug
Copy link

@scabug scabug commented Jun 22, 2011

I've got a compiler error when trying to implement a java interface which has a method with vararg parameter using a anonymous class. It is strange 'cause it only happens when i explicit define its type. See example bellow:

// Java Interface: 
public interface VarArgs {
  public void method(String... s);
} 
 
// Scala class: 
class ScalaVarArgs extends VarArgs { 
  // -- no problem on overriding it using ordinary class 
  def method(s: String*) { println(s) } 
} 

object ScalaVarArgs { 
  def main(args: Array[String]) { 
    //[1] Ok - no problem using inferred type 
    val varArgs = new VarArgs { 
      def method(s: String*) { println(s) } 
    } 
    varArgs.method("1", "2") 

    //[2] Ok -- no problem when explicit set its type after construction
    val b: VarArgs = varArgs 
    b.method("1", "2") 
    
    //[3] Ok -- no problem on calling its method
    (new ScalaVarArgs).method("1", "2") 
    (new ScalaVarArgs: VarArgs).method("1", "2") 
    
    //[4] Not Ok -- error when assigning anonymous class to a explictly typed val
    // Compiler error:  object creation impossible, since method method in trait VarArgs of type (s: <repeated...>[java.lang.String])Unit is not defined 
    val tagged: VarArgs = new VarArgs { 
      def method(s: String*) { println(s) } 
    } 
  } 
} 

Note: this error/doubt was original post at scala-users mail list: http://groups.google.com/group/scala-user/browse_thread/thread/85757cea2cd25069/27817e912c89a6f5?lnk=gst&q=fabio.veronez#27817e912c89a6f5

@scabug
Copy link
Author

@scabug scabug commented Jun 22, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4729?orig=1
Reporter: Fabio (fabio.veronez)

@scabug
Copy link
Author

@scabug scabug commented Mar 12, 2012

Sarah Gerweck (gerweck) said:
I am encountering this exact same bug in Scala 2.9.1 final. It's quite an annoying issue!

@scabug
Copy link
Author

@scabug scabug commented Oct 1, 2012

Alexandre Cardoso (accbel) said:
Same error when trying following case

class A {
  def otherMethod(arg: VarArgs) {
    //A piece of code
  }
}

// Not Ok -- error when assigning anonymous class as a method parameter
// Compiler error:  object creation impossible, since method method in trait VarArgs of type (s: <repeated...>[java.lang.String])Unit is not defined
new A().otherMethod(new VarArgs {
  def method(s: String*) { println(s) }
}

@scabug
Copy link
Author

@scabug scabug commented Oct 1, 2012

@paulp said:
I submitted a fix a couple days ago; it's not merged yet. scala/scala#1430

@scabug
Copy link
Author

@scabug scabug commented Jan 23, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants