groovy Closures Create closures, assign to properties and call


Example

Let's create a map and a closure to print hello

def exMap = [:]

def exClosure = {
    println "Hello"
}

Assign closure to a property in map

exMap.closureProp = exClosure

Calling closure

exMap.closureProp.call()

Output

Hello

Another Example - Lets create a class with basic property and assign same closure to object of it

class Employee {
    def prop
}

def employee = new Employee()

employee.prop = exClosure

Call closure through that property

employee.prop.call()

Output

Hello