15

Here's an interesting discussion of Tennent's Correspondence Principle, and a brief description from Neal Gafter:

The principle dictates that an expression or statement, when wrapped in a closure and then immediately invoked, ought to have the same meaning as it did before being wrapped in a closure. Any change in semantics when wrapping code in a closure is likely a flaw in the language.

Does the Groovy language follow this principle?

Armand
  • 6,508
  • 4
  • 37
  • 53

1 Answers1

4

Not quite, but almost. All the variables accessible in the outer scope are accessible in the closure, including 'this' and class data members. However, a return statement returns from the closure, not from the enclosing function. I'm not even sure that is a violation of TCP; I don't know of any C-style language where return in a closure returns from the enclosing scope. I have had no surprises using Groovy closures.

kevin cline
  • 33,608
  • 3
  • 71
  • 142
  • Would be -1 if I had sufficient rep, see further in the link from OP: http://programmers.stackexchange.com/questions/116395/what-is-the-good-explanation-of-tennents-correspondence-principle#120409 – sq33G Dec 18 '11 at 08:35