Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Included the values printed by the various enclosure examples. I think the fun(123) example is out of place here.

...

Code Block
java
java
def fun(int i, Closure c) {
  c.call(i)
}

// put Closure out of ()
[1, 2, 3].each() { item -> print "${item}-" } // 1-2-3-
fun(123) { i -> println i } // 123

// omit ()
[1, 2, 3].each ({ item -> print "${item}-" }) // 1-2-3-

// omit enclosing ()
[1, 2, 3].each { item -> print "${item}-" } // 1-2-3-

// normal
[1, 2, 3].each(({ item -> print "${item}-" })) // 1-2-3-

def closure = { i -> println i}

//[1, 2, 3].each() closure // error. closure has been defined

...