Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7011

Trait methods with default arguments do not work correctly

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.6
    • Fix Version/s: 2.3.10, 2.4.0-rc-1
    • Component/s: None
    • Labels:
      None
    • Environment:
      OSX 10.9, Java 1.7_67

      Description

      The following code:

      trait HiSupport {
         def sayHi(String msg = "hi") {
             println msg
         }
      }
      
      class A implements HiSupport { }
      
      new A().sayHi()
      

      Produces incorrect results. The trait method is called without any dispatching complaints, however it just ignores the default argument.

      If default arguments are not supported, it seems like it should be a compile failure or at least a failure during dispatch.

      If the trait method has multiple arguments, the method dispatch does result in a failure:

      trait HiSupport {
         def sayHi(String name, String msg = "hi") {
             println msg + name
         }
      }
      
      class A implements HiSupport { }
      
      new A().sayHi("Bob")
      
      groovy.lang.MissingMethodException: No signature of method: A.sayHi() is applicable for argument types: (java.lang.String) values: [Bob]
      Possible solutions: sayHi(java.lang.String, java.lang.String), wait(), any(), each(groovy.lang.Closure), asType(java.lang.Class), wait(long)
      	at ConsoleScript50.run(ConsoleScript50:9)
      

        Attachments

          Activity

            People

            • Assignee:
              melix C├ędric Champeau
              Reporter:
              longwa Aaron Long
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: