groovy switchステートメントでClosureを判定 2007/07/16

groovyのswitchです。
いろいろなものがcaseに使えて便利ですね。



ふとクロージャーもCaseとして判定できるのかなと思って試してみました。
判定できますね。



class test_switch {

static void main(args) {

def c = {
println "i am closure ${it}"
}
a(c)
a('inList')
a(17)
}

static void a(x){
def result = ""

switch ( x ) {
case "foo":
result = "found foo"
break
case [5, 6, 7, 'inList']:
result = "list"
break
case 12..30:
result = "range"
break
case Integer:
result = "integer"
break
case Number:
result = "number"
break
case Closure:
result = "closure"
break
default:
result = "default"
}

println result
}

}

: