September 11, 2009

Groovy Goodness: Optional return Keyword

In Groovy we can omit the return keyword at the end of a method, it is optional. It is no problem to leave it in, but we can leave it out without any problems. Since Groovy 1.6 this is even true when the last statement of a method is a conditional statement or a try-catch block.

def simple() {
    "Hello world"
assert 'Hello world' == simple()

def doIt(b) {
    if (b) {
        "You are true"
    } else {
        "You are false"
assert 'You are true' == doIt(true)
assert 'You are false' == doIt(false)

def tryIt(file) {
    try {
        new File(file).text
    } catch (e) {
        "Received exception: ${e.message}"
    } finally {
        println 'Finally is executed but nothing is returned.'
        'Finally reached'
assert 'Received exception: invalidfilename (The system cannot find the file specified)'  == tryIt('invalidfilename') 
// Create new file with the name test.
def newFile = new FileWriter('test').withWriter { it.write('file contents') }
assert 'file contents' == tryIt('test')