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 - Groovy Tutorial
From the course: Learning Groovy
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.