groovy

Inline conditional List << not being executed fully.

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: 1.0-JSR-6
  • Fix Version/s: 1.1-rc-1
  • Component/s: None
  • Labels:
    None
  • Environment:
    WinXP SP2
    Groovy Version: 1.0-RC-01-SNAPSHOT JVM: 1.4.2_08-b03
  • Testcase included:
    yes
  • Number of attachments :
    0

Description

Inline Tertiary (boolean ? x : y) and List.add operator << execution bug

I don't know if this is a parsing issue or a precedence issue but the following code does not act as expected:

TestCase:
----------------------------------
def list = []
list << (true) ? "foo" : "bar"
println list
----------------------------------

Output:
----------------------------------
[true]
----------------------------------

I would have expected to see ["foo"] in this case.

Forcing execution with parens seems to be an easy workaround such as:
list << ((true) ? "foo" : "bar") // list == ["foo"]

Activity

Hide
Ryan Misek added a comment -

After looking at the JavaDoc for the List interface, List.add returns a boolean value so it looks to be more of an operator precedence issue.

so at frist glance it seems to be executing like:
(list << (true)) ? "foo" : "bar"

then the "foo" just gets dropped off into no man's land while true is actually added to the list as a side-effect

Show
Ryan Misek added a comment - After looking at the JavaDoc for the List interface, List.add returns a boolean value so it looks to be more of an operator precedence issue. so at frist glance it seems to be executing like: (list << (true)) ? "foo" : "bar" then the "foo" just gets dropped off into no man's land while true is actually added to the list as a side-effect
Hide
Paul King added a comment -

Suggest we don't fix this. Groovy resembles Java precedence order here which has ?: at 14 (almost lowest) and << at 6 (just above midway). So, any changes to switch these two around would represent a massive upheaval in precendence order. Just use brackets for this case.

Show
Paul King added a comment - Suggest we don't fix this. Groovy resembles Java precedence order here which has ?: at 14 (almost lowest) and << at 6 (just above midway). So, any changes to switch these two around would represent a massive upheaval in precendence order. Just use brackets for this case.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: