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

Using the + operator to concatenate objects with a string does not call toString()

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • None
    • 1.0-RC-1
    • None
    • None

    Description

      This test case fails with the exception below:

      void testInvokeToStringOnPlus()

      { def obj = new ToString(val:"some string") assert obj + " + another string" == "some string + another string" }

      class ToString {
      @Property val
      public String toString()

      { return val }

      }

      testInvokeToStringOnPlus(StringTest)groovy.lang.MissingMethodException: No signature of method ToString.plus() is applicable for argument types: (java.lang.String) values:

      {" + another string"}

      If plus(String) is not overriden by the class being concatenated with the + operator, it should call toString() instead.

      Attachments

        Activity

          to convert the Object to string, if the Object is on the left side might look reasonable, but it won't work as suspected if a subclass overwrites plus for String. having to write ""obj"aString" is not that bad. If you don't want to, use GStrings: "$obj aString"

          blackdrag Jochen Theodorou added a comment - to convert the Object to string, if the Object is on the left side might look reasonable, but it won't work as suspected if a subclass overwrites plus for String. having to write "" obj "aString" is not that bad. If you don't want to, use GStrings: "$obj aString"

          People

            blackdrag Jochen Theodorou
            meling Hein Meling
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: