Groovy Truth (true-ness) – Overriding boolean evaluation in a user defined class

Sometimes it may be useful to have a specific asBoolean definition in your own program for some kind of objects.

/** an oversimplified robot controller */
class RunController {
    
    def complexCondition
    int position = 0
    
    def asBoolean() {
        return complexCondition(this);
    }
    def advanceTo(step) {
        position += step
    }
}
def runController = new RunController(complexCondition : { c -> c.position < 10 } )

assert runController
runController.advanceTo(5)
assert runController
runController.advanceTo(5)
// The limit has been reached : the controller evaluates to false
assert !runController

This code shows an oversimplifed robot controller who checks that the position of the robot does not exceeds 10 (with a closure for condition evaluation)

if you want to reproduce, please indicate the source:
Groovy Truth (true-ness) – Overriding boolean evaluation in a user defined class - CodeDay