groovy Ternary and Elvis Operators Standard form vs Elvis form

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

// 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.



Got any groovy Question?