groovy

Ternary and Elvis Operators

Remarks#

The Elvis operator evaluates based on Groovy-Truth of the condition-part.

Standard form vs Elvis form

// long form
String sayHello(String name){
    "Hello, ${name ? name : 'stranger'}."
}

// elvis
String sayHello(String name){
    "Hello, ${name ?: 'stranger'}."
}

Notice that the “elvis” format omits the “true” term because the original comparison value is to be used in the “true” case. If name is Groovy true, then it will be returned as the value of the expression.

Usage (with condition) in assignment

def results = []
(1..4).each{
    def what = (it%2) ? 'odd' :  'even'
    results << what
}
assert results == ['odd', 'even', 'odd', 'even']

Here, the if-condition (in (parentheses)) is slightly more complex than just testing for existence/Groovy-Truth.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow