Scala Programming Language
  1. Scala Programming Language
  2. SI-4729

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

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Scala 2.10.1
    • Component/s: Misc Compiler
    • Environment:

      Description

      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

        Activity

        Hide
        Sarah Gerweck added a comment -

        I am encountering this exact same bug in Scala 2.9.1 final. It's quite an annoying issue!

        Show
        Sarah Gerweck added a comment - I am encountering this exact same bug in Scala 2.9.1 final. It's quite an annoying issue!
        Hide
        Alexandre Cardoso added a comment -

        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) }
        }
        
        
        Show
        Alexandre Cardoso added a comment - 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) } }
        Hide
        Paul Phillips added a comment -

        I submitted a fix a couple days ago; it's not merged yet. https://github.com/scala/scala/pull/1430

        Show
        Paul Phillips added a comment - I submitted a fix a couple days ago; it's not merged yet. https://github.com/scala/scala/pull/1430
        Hide
        Adriaan Moors added a comment -
        Show
        Adriaan Moors added a comment - fixed for 2.11 in https://github.com/paulp/scala/commit/32e70a01da1fda7bdc91d7301ee3b8707fd2bcd4 – potential backport pending

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Fabio
          • Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development