Groovy Closing Concept

I am a little confused about the term "closure" used in the Groovy documentation. According to the documentation, their definition of closure seems more like an anonymous function or lambda.

I understand that languages ​​that support first-class functions usually allow you to create closures. However, there is a difference between the two.

For example, according to Groovy docs:

Closing in Groovy is an anonymous piece of code that can take arguments, return a value, and also specify and use variables declared in the surrounding realm.

In the language of a functional language, such an anonymous block of code can be referred to as an anonymous lambda expression as a whole or a lambda expression with unrelated variables or a closed lambda expression if it does not contain references to unrelated variables (for example, a threshold in an earlier example). Groovy makes no such difference.

Then according to Wikipedia on the Closures page:

The term closure is often mistakenly used to mean an anonymous function.

What am I missing?

+8
closures groovy
source share
1 answer

As the next paragraph of the same page that you linked to groovy docs says :

Strictly speaking, closure cannot be defined. You can define a code block that refers to local variables or fields / properties, but it becomes closure only when you β€œbind” (give it a value) this code block for variables. Closing is a semantic concept, like an instance that you cannot define, just create. Strictly It is said that closure is only closure if all free variables are connected. If this does not happen, it is only partially closed, therefore, it is not really a closure. Since groovy does not provide a way to define a closed lambda function and the code block may not be a closed lambda function at all (because it is free variables), we call closure - even as a syntax concept. We talk about this as a syntactic concept, since the definition code and instantiation are one, there is no difference. We know very well that this terminology is more or less wrong, but it simplifies many things when talking about code in a language that does not "know" the difference.

+11
source share

All Articles