Wednesday, October 24, 2007

A Groovy switch/case example

Below is an example of Groovy's switch/case feature taken from The Definitive Guide to Grails by Graeme Rocher. I am not a regular user of Java's switch/case feature, but if it had been this way perhaps I might have.


switch (x) {
case 'James':
println "yes it is me"
break
case 18..65:
println "ok you are old"
break
case ~/Gw?+e/:
println "your name starts with G and ends in e!"
break
case Date:
println 'got a Date instance'
break
case ['John', 'Ringo', 'Paul', 'George']:
println "Got one of the Beatles"
break
default:
println "Don't know"
}

5 comments:

Kit Plummer said...

Wow, a switch on "strings". The reason you don't use 'em in Java is because Java is lame.

products said...

China Wholesale has been described as the world’s factory. This phenomenom is typified by the rise ofbusiness. Incredible range of products available with China Wholesalers “Low Price and High Quality” not only reaches directly to their target clients worldwide but also ensures that wholesale from china from China means margins you cannot find elsewhere and buy products wholesaleChina Wholesale will skyroket your profits.wedding dressescheap naruto cosplayanime cosplaycheap Gemstone Jewelry

products said...

Women’s nike tn Shox Rivalry est le modèle féminin le plus tendance de baskets pour le sport. tn chaussuresConcernant la semelle :spyder jacketsCheap Brand Jeans Shop - True Religion Jeans cheap nike shox & Puma Shoes Online- tn nike,Diesel Jeans le caoutchouc extérieur, l’EVA intermédiaire Levis Jeanset le textile intérieur s’associent pour attribuer à la.ed hardy shirts pretty fitCharlestoncheap columbia jackets. turned a pair of double plays to do the trick.Lacoste Polo Shirts, puma basket, Burberry Polo Shirts.wholesale Lacoste polo shirts and cheap polo shirtswith great price.Thank you so much!!cheap polo shirts men'ssweate,gillette mach3 razor bladesfor men.As for Cheap Evisu JeansCheap Armani Jeanspolo shirtsPuma shoes

Anonymous said...

You'll never see 'George' as a Beatle with this code ... is that an implied opinion? ;)

dewdrop said...

"Kit Plummer said...

Wow, a switch on "strings". The reason you don't use 'em in Java is because Java is lame."


Dude,

modern Groovy switches are cool but 100 times slower than java if-else-ladder...

reverse of what happens in c/c++
(switches are faster than if-else)

looks like one can't have the cake and eat it too