...
Non-breaking Switch Enhancements
Objective: get rid of a lot of if then else statements; eliminate unnecessary syntax; no impact on "java-like" groovy code.
Multiple parameters/targets
| Code Block |
|---|
// multiple parameter switch
// '?' == true/any
def x = { a, b -> switch(a,b) {
case (0, 0) : return 0
case (0, 1), (1, 0) : return 1
case (1, ?), (?, 1) : return 2
default : return 3
}}
|
Alternate '->' Syntax, Optional 'default', Assign Result
| Code Block |
|---|
// in a switch '->' implies break,
// 'default' is optional,
// and they can have a result
def x = { a ->
switch (a) {
case 0 -> println 'a'
case 1 -> println 'b'
-> println 'c'
}
def b = switch(a) {
case 0 -> 'a'
case 1 -> 'b'
-> 'c'
}
}
|
Combine "Multiparameter" and "Alternate Syntax" with Abbreviated form in Closures
| Code Block |
|---|
// a short form of the above for function pattern matching
def x = { a, b
case (0, 0) -> 0
case (0, 1),
(1, 0) -> 1
case (1, ?),
(?, 1) -> 2
default -> 3
}
|
Here the '->' form implies 'return' instead of 'break' (same result at this level)
Another example:
| Code Block |
|---|
// case works as it does currently, executing the closure or calling isCase() def qsort = { ls case {ls.size() < 2} -> ls -> def (x, xs) = [ls.head(), ls.tail()] def smaller, larger = [] xs.each { it<=x ? smaller<<it : larger<<it } qsort(smaller) + x + qsort(larger) } |
...
The above proposal does not break any existing code,
does not add any keywords, does not break compatibility
with java ("paste java in to groovy and it work", etc).
The multi-parameter switch statement and '->' in a switch so it has a result
is where the core of the work would be. The 'case' statement on a closure definition
for pattern matching like capability is just syntactical sugar.
--krsmes (Apr 2010)
...
- New function for integers to reverse bits.
- Concatenate string with null object. 'Test'+null will produce 'Test'.
- Function/Global method 'NVL'. This method avoid null/empty string value of object. E.g. NVL(null,'Test')='Test' and NVL('','Test')='Test'
- Joining of list without null elements.
...