groovy
  1. groovy
  2. GROOVY-1299

Confusing and dangerous String->int conversion

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: 1.0-JSR-4
    • Fix Version/s: None
    • Component/s: syntax
    • Labels:
      None
    • Environment:
      JSR4
    • Number of attachments :
      0

      Description

      String s = "5"
      int x = s

      The effect of this code is that x is assigned the value of char '5'.

      String s = "10"
      int x = s

      This produces a runtime exception.

      If auto-conversion is absolutely needed between 1-character Strings and ints, then one would hope it would at least be limited to literal Strings. This behavior makes no sense.

        Activity

        Hide
        blackdrag blackdrag added a comment -

        this is no bug, this is the intended behavior. That is because groovy doesn't know characters, but handles characters as Strings of length 1. The ways I see to solve the problem above is to introduce chars or to forbid the handling of Strings of length 1 as chars when not explicitly converted to chars before.

        String s = "5"
        int x = s

        would then throw an runtime exception.

        String s = "5"
        char c = s
        int x = c

        would not.

        String s = "10"
        int x = s

        and String s = "10"
        char c = s

        would both cause an exception during runtime.

        BTW, after the String is created, there is no way to know it was a liteal String.

        Show
        blackdrag blackdrag added a comment - this is no bug, this is the intended behavior. That is because groovy doesn't know characters, but handles characters as Strings of length 1. The ways I see to solve the problem above is to introduce chars or to forbid the handling of Strings of length 1 as chars when not explicitly converted to chars before. String s = "5" int x = s would then throw an runtime exception. String s = "5" char c = s int x = c would not. String s = "10" int x = s and String s = "10" char c = s would both cause an exception during runtime. BTW, after the String is created, there is no way to know it was a liteal String.
        Hide
        John O'Malley added a comment -

        Is there a downside to your second solution ("forbid the handling of Strings of length 1 as chars without explicit conversion")?

        You could resolve literals at parse time, whenever possible. So:

        int x = "5"

        would be the same as int x = '5' in Java, but

        int x = foo

        would fail at runtime whenever foo is a String of any length.

        Show
        John O'Malley added a comment - Is there a downside to your second solution ("forbid the handling of Strings of length 1 as chars without explicit conversion")? You could resolve literals at parse time, whenever possible. So: int x = "5" would be the same as int x = '5' in Java, but int x = foo would fail at runtime whenever foo is a String of any length.
        Hide
        blackdrag blackdrag added a comment -

        there are always downsides... for example old code will behave different. More weight does have that it will not be possible to do something like this:

        def foo(char c){}

        foo("a")

        This must fail then, because "a" is a String and no char.

        adding special handling for

        int x="5"

        is no good idea. People normally don't care much about String constants and String references when coming from Java. And it will be hard to explain them, that

        int x="5"

        behaves different from

        def foo()

        {"5"}

        int x = foo()

        Show
        blackdrag blackdrag added a comment - there are always downsides... for example old code will behave different. More weight does have that it will not be possible to do something like this: def foo(char c){} foo("a") This must fail then, because "a" is a String and no char. adding special handling for int x="5" is no good idea. People normally don't care much about String constants and String references when coming from Java. And it will be hard to explain them, that int x="5" behaves different from def foo() {"5"} int x = foo()
        Hide
        John O'Malley added a comment -

        I don't have any good ideas, so I guess I'll close this issue. I'm assuming that's OK - if it's the assignee's job to close it I apologize.

        Show
        John O'Malley added a comment - I don't have any good ideas, so I guess I'll close this issue. I'm assuming that's OK - if it's the assignee's job to close it I apologize.

          People

          • Assignee:
            Guillaume Laforge
            Reporter:
            John O'Malley
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: