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

Inconsistent setter selection dynamic and static Groovy

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.3
    • 2.4.0-beta-4
    • None
    • None

    Description

      Tested in JDK 7, GroovyConsole 2.3.3:

      class X {
        void setValue(String v) { println "String $v" }
        void setValue(float f) { println "float $f" }
      }
      
      new X().value = 12.3f //Outputs "float 12.3"
      //new X().value = "123" //would fail with Cannot cast object '123' with class 'java.lang.String' to class 'float'
      
      @groovy.transform.CompileStatic
      void doIt() {
      new X().value = 12.3f //Outputs "String 12.3"
      new X().value = "123" //Outputs "String 123"
      }
      
      doIt()
      

      In real code, some developers report code always working/compiling, others report that it fails 25% of the time.

      I expected dynamic Groovy to select method at runtime based on type, and static Groovy to select method at compile time based on inferred type.

      I think this is a bug in Groovy, if for no other reason than dynamic Groovy ought to issue an ambiguous call and static Groovy should at a minimum issue compile error, if Groovy doesn't support multiple setX methods when using property notation. Or maybe at least Groovy should be consistently selecting a set method. Based on the other developer's experience, it seems that the setter selection could differ on each execution. In any case I'sd expect a compile/runtime error or consistent behavior.

      Attachments

        Activity

          People

            melix C├ędric Champeau
            gillius Jason Winnebeck
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: