groovy
  1. groovy
  2. GROOVY-2994

String and GString do not commute under plus()

    Details

    • Type: Bug Bug
    • Status: Reopened Reopened
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.5.6
    • Fix Version/s: 3.0
    • Component/s: groovy-runtime
    • Labels:
      None
    • Number of attachments :
      0

      Description

      assert "$

      {1}

      " + "2" instanceof GString
      assert "1" + "$

      {2}

      " instanceof String

      As far as I can see plus() should be commutative throughout the GDK. At least it should be commutative for String and GString.

        Activity

        Hide
        blackdrag blackdrag added a comment -

        this is not a critical bug, so I reduce the priority

        Show
        blackdrag blackdrag added a comment - this is not a critical bug, so I reduce the priority
        Hide
        Roshan Dawrani added a comment -

        This issue is not there anymore. Checked on all 3 branches (1.5, 1.6, 1.7).

        Show
        Roshan Dawrani added a comment - This issue is not there anymore. Checked on all 3 branches (1.5, 1.6, 1.7).
        Hide
        Guillaume Laforge added a comment -

        So let's close it

        Show
        Guillaume Laforge added a comment - So let's close it
        Hide
        Alexander Veit added a comment -

        I'm afraid the the script that shows the bug still works for me in 1.6-RC1 and 1.5.7.

        Show
        Alexander Veit added a comment - I'm afraid the the script that shows the bug still works for me in 1.6-RC1 and 1.5.7.
        Hide
        blackdrag blackdrag added a comment -

        Alexander, could you try trunk and the 1.6 branch?

        Show
        blackdrag blackdrag added a comment - Alexander, could you try trunk and the 1.6 branch?
        Hide
        Alexander Veit added a comment -

        With both tunk and GROOVY_1_6_X, rev. 14993, the two asserts

        assert "${1}" + "2" instanceof GString
        assert "1" + "${2}" instanceof String
        

        work. So GString and String do not commute under plus.

        I wonder if I'm doing something wrong...

        Show
        Alexander Veit added a comment - With both tunk and GROOVY_1_6_X, rev. 14993, the two asserts assert "${1}" + "2" instanceof GString assert "1" + "${2}" instanceof String work. So GString and String do not commute under plus. I wonder if I'm doing something wrong...
        Hide
        blackdrag blackdrag added a comment -

        ok, I see... the actual problem is not that the test should pass, but that it does pass... You say that if String+GString gives T, then GString+String should give T as well. Ok, what should T be and why?

        Show
        blackdrag blackdrag added a comment - ok, I see... the actual problem is not that the test should pass, but that it does pass... You say that if String+GString gives T, then GString+String should give T as well. Ok, what should T be and why?
        Hide
        Alexander Veit added a comment -

        ... and why?

        The current behavious can lead to surprising errors. E.g. when object identity comes into play as shown here

        def u = []
        def v = 'foo'
        
        u << "${v}" + "bar"
        u << "bar" + "${v}"
        
        assert u.contains("foobar") == false
        assert u.contains("barfoo") == true
        

        or with lazy closure evaluation as shown here

        int i
        def g
        def s
        def t
        
        Closure c = {-> i}
        g = "$c"
        
        i = 1
        s = g + "--"
        i = 2
        t = g + "--"
        assert s == t
        
        i = 1
        s = "--" + g
        i = 2
        t = "--" + g
        assert s != t
        

        what should T be

        That depends on the language spec

        For the sake of simplicity, for implementation and user, I would tend to T = String. This would be probably also be more compatible to other operations such as leftShift.

        Show
        Alexander Veit added a comment - ... and why? The current behavious can lead to surprising errors. E.g. when object identity comes into play as shown here def u = [] def v = 'foo' u << "${v}" + "bar" u << "bar" + "${v}" assert u.contains( "foobar" ) == false assert u.contains( "barfoo" ) == true or with lazy closure evaluation as shown here int i def g def s def t Closure c = {-> i} g = "$c" i = 1 s = g + "--" i = 2 t = g + "--" assert s == t i = 1 s = "--" + g i = 2 t = "--" + g assert s != t what should T be That depends on the language spec For the sake of simplicity, for implementation and user, I would tend to T = String. This would be probably also be more compatible to other operations such as leftShift.

          People

          • Assignee:
            Unassigned
            Reporter:
            Alexander Veit
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: