From the course: Learning Groovy

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Understanding Closures

Understanding Closures - Groovy Tutorial

From the course: Learning Groovy

Start my 1-month free trial

Understanding Closures

- [Instructor] If you have a deeper look at the GDK API documentation, you will find a specific type of called groovy.lang.Closure, a common functional programming structure available in other languages. So what exactly is a Closure? It's an anonymous block of code that can span multiple lines defined within curly braces. What's the use case of Closure, you might ask? To write shorter, more concise code. Moreover, Closures are central to writing and using domain specific languages. They're used heavily in conjunction with collection types, a topic we'll have a look at later in this chapter. With this background information in mind, let's have a look at a concrete example. Say we wanted to implement a Closure that prints the string representation of a person. For that purpose, we'll reference the existing Person instance and call the toString method on it. Now to make this statement a Closure, you just have to wrap it…

Contents