четверг, 1 августа 2013 г.

Fun with groovy maps and function call syntax

A function having a Map as first parameter:
1
2
3
4
void doIt(Map attrs, Object content) {
  println attrs
  println content
}
supports equally valid call syntax variations:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// "classical" call syntax, known from java world
doIt([color: 'red', type: 'fruit'], 'hello!')

// parentheses can be omitted
doIt [color: 'red', type: 'fruit'], 'hello!'

// even square brackets for map can be omitted
doIt color: 'red', type: 'fruit', 'hello!'

// order of map properties does not matter,
// map properties can be intermixed with unnamed parameters.

doIt color: 'red', 'hello!', type: 'fruit'

doIt 'hello!', type: 'fruit', color: 'red'
this effectively allows to implement named parameters in groovy.

Комментариев нет:

Отправить комментарий