Groovy: Difference between revisions
Jump to navigation
Jump to search
(→TODO) |
|||
Line 26: | Line 26: | ||
<syntaxhighlight lang='groovy'> | <syntaxhighlight lang='groovy'> | ||
def myClosure = { e -> println "Clicked on $e.source" } | def myClosure = { e -> println "Clicked on $e.source" } | ||
</syntaxhighlight> | |||
Passing a closure to a method: | |||
<syntaxhighlight lang='groovy'> | |||
file.eachLine(line -> { | |||
println line | |||
}) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 06:24, 21 November 2019
TODO
- Tutorial: www.tutorialspoint.com/groovy/index.htm
TODO Groovy basics: https://docs.gradle.org/current/userguide/writing_build_scripts.html#groovy-dsl-basics
String Programming
TODO next time I need it.
Single-Quoted vs. Double-Quoted Strings
Groovy has both double-quoted and single-quoted String literals. The main difference is that double-quoted String literals support String interpolation:
def x = 10
println "result is $x" // prints: result is 10
Working with Closures
Defining a Closure
def myClosure = { e -> println "Clicked on $e.source" }
Passing a closure to a method:
file.eachLine(line -> {
println line
})
Implicit Paramenter
When a closure does not explicitly define a parameter using the '->' syntax, the closure 'always defines an implicit parameter named "it".
Passing Closures to Methods
If the closure is the last argument for a method, it can be passed outside the argument list.
Template Engines
Files
filename = 'example.txt'
File f = new File(filename)
def lines = f.readLines()
for (line in lines) {
// ...
}
filename = 'example.txt'
File f = new File(filename)
f.eachLine({
if (!it.startsWith("#") && !it.trim().isEmpty()) {
...
}
})