Показаны сообщения с ярлыком map. Показать все сообщения
Показаны сообщения с ярлыком map. Показать все сообщения

вторник, 6 августа 2013 г.

Partial interface implementation in groovy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
interface X {
  void a()
  void b()  
}

class XAdapter implements X {
  void a() { println 'default implementation of a' }
  void b() { println 'default implementation of b' }
}

def o = [ 
  a: { println 'overridden implementation of a' } 
] as XAdapter

o.a()
o.b()
will output:
overridden implementation of a
default implementation of b

четверг, 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.