Skip to content

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

@scabug

Description

@scabug

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions