History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GROOVY-1733
Type: Improvement Improvement
Status: Open Open
Priority: Minor Minor
Assignee: Unassigned
Reporter: Siegfried Puchbauer
Votes: 1
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
groovy

Let boolean operators (||, &&) evaluate to the value instead of the boolean equivalent

Created: 15/Feb/07 06:22 AM   Updated: 26/Apr/08 08:11 AM
Component/s: syntax
Affects Version/s: None
Fix Version/s: 2.0

Time Tracking:
Not Specified


 Description  « Hide
It would enable you to do some things a lot faster (eg. variable declaration) and would improve the current null-save navigation

examples:

def a = 0
def b = null
def c = "a string"
def d = ""

def val = a || c // c gets evalutated, because the boolean equivalent of a (0) is false

assert a || b == null
assert c || d == "a string"
assert d || a == 0
assert a || "another string" == "a string"
assert d || "another string" == "another string"

for null-save navigation:

def val = aMap?.aProperty?.aSubProperty || "a default Value"

or even for causal execution of code

myobject.aMethodThatReturnsNullWhenItFails() || myobject.anotherMethod() 
// myobject.anotherMethod gets only executed the result of the first call evalutes to false

The current notation would not be affected in a negative way

if(a || b){...} // would still work

you would still have the possibility to get the boolean value by using !!

eg.

assert (0 || 15) instanceof Integer
assert !!(0 || 15) instanceof Boolean

The only language I know, which supports this currently is JavaScript



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
There are no comments yet on this issue.