groovy

GString as String[] returns a 1-element array with the whole text

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0, 1.1-beta-1, 1.1-beta-2, 1.1-beta-3
  • Fix Version/s: 1.1-rc-1
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    0

Description

This is an old bug.
Case 1)
groovy> String str= "hello 10"
groovy> str as String[]
Result: ["h", "e", "l", "l", "o", " ", "1", "0"]

That is, an array of String with one element per character in the original String.

Case 2)
groovy> def i= 10
groovy> def gstr= "hello $i"
groovy> gstr as String[]
Result: ["hello 10"]

That is, an array of String, with only one element consisting of the whole rendered String.

This is inconsistent behaviour and may give undesired results when calling methods that expect an array of String

Activity

Hide
Paul King added a comment -

Fixed in head with the following test:
String s = 'hello 10'
def gs = "hello {5+5}"
def expected = ["h", "e", "l", "l", "o", " ", "1", "0"]
assert s.toList() == expected
assert s as String[] == expected
assert gs as String[] == expected

Show
Paul King added a comment - Fixed in head with the following test: String s = 'hello 10' def gs = "hello {5+5}" def expected = ["h", "e", "l", "l", "o", " ", "1", "0"] assert s.toList() == expected assert s as String[] == expected assert gs as String[] == expected

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: