Dark theme | Light theme

June 7, 2018

Groovy Goodness: Use Optional In Conditional Context

In Groovy 2.5.0 we can use a Java 8 Optional object in a conditional context. When an Optional object has a value the value is coerced to true and when empty the value is false.

assert !Optional.empty()
assert !Optional.ofNullable(null)
assert Optional.of('Groovy rocks!')

Written with Groovy 2.5.0.