-
Notifications
You must be signed in to change notification settings - Fork 0
/
Implicits.groovy
51 lines (38 loc) · 933 Bytes
/
Implicits.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package impl.groovy
import org.junit.Test
/**
* Created by pabloperezgarcia on 16/10/2016.
*/
class Implicits {
def value = 5
def setDouble = { i = value -> i * 2 }
@Test
def void main() {
println setDouble(6) // Got 6
println setDouble() // Got 5
value = 0
println setDouble() // Got 0
}
@Test
def void customString() {
println "Welcome implicits world in groovy".upperCaseNoSpace
println "Welcome implicits world in groovy".removeSpaces
}
static {
String.metaClass.getUpperCaseNoSpace = {
delegate.toUpperCase().replace(" ", "_")
}
String.metaClass.getRemoveSpaces = {
delegate.replace(" ", "")
}
}
@Test
def void numbers() {
println 2.doubleVal
}
static {
Integer.metaClass.getDoubleVal = {
2 * delegate
}
}
}