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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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' ) |