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"
}

26 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

Chang bui thi said...
This comment has been removed by the author.
friv said...

This is extremely helpful info!! Very good work. Everything is very interesting to learn and easy to understood. Thank you for giving information.
Signature:
i like play games games girls Download baixar facebook movel

Ngoc Ha said...


Thanks for sharing this quality information with us. I really enjoyed reading. I think I need it.
Games for girls|
Kizi 1|
Kizi 2

thị hậu nguyễn said...

I do think that is amongst the most interesting articles or blog posts I’ve stay with me that topic. You have made your current factors exciting, unique as well as I accept nearly all. My business is delighted I discovered your current article these days.
happy wheels
super mario bros
pacman
agario

bibikafu said...

Hello to every body, it's my first pay a quick visit of this weblog; this weblog carries amazing and really excellent data in favor of readers.
Friv 4
Kizi 3

ka maz said...

you always bring everyone the most interesting and useful, I like it all, thank you.
Friv 10

Yepi said...

You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
Thanks you for sharing!
Yepi
Kizi
Friv
Friv Games

Jasmine Thompson said...

agario|agar io|agar.io|agar|agario game|agario skins|agario extended|agario mods|agario|agar io

tranbaongoc said...

You are stressed and tired, you need entertainment. Refer to our website. hope you get the most comfortable.
Kizi 4

Friv10 said...

you'd have time to look these kids active. Please visit our website and let us play the game interesting.
Thanks for sharing !
Friv 10
Kizi 10
Yepi 2

Friv5 said...

You need to kill time, you'd have time to look these kids active. Refer to our website. Hope you get the most comfort.
Thanks for sharing !
Friv 5
Kizi 1
Yepi 3

tranthuphuong said...

thank you for your interest in welcome visit
Friv 4

tranduyphong said...

New attractive game . I will play it every day
Friv|
Friv 2

Imran Hashmi said...

I’ll make sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I will certainly return.
Malwarebytes Anti Malware
Comodo Internet Security 2016
Microsoft Office 365
Camfrog Video Chat
Driver Genius 16

Friv4 said...

When you're tired, you want to relax after a stressful working hours, you need to have time to take care of the kids active.
Please visit our website and play exciting flash games.Thanks you for sharing!
Friv 4

Kizi200 said...

Play the Best Free Games! We've picked out the racing games, cooking games, candy crush, games shooting, fashion games, ...
Thanks for sharing !
Kizi 200
Friv 1
Y88
Y9

lee anh nuooi said...

invites you to play games for a chance to win cars Corolla Altis car


Friv Games
Kizi 100
Games2Win
Kizi 5

Friv100 said...

You want to relax after a stressful working hours. Refer to our website. hope you get the most comfort.
Thanks for sharing !
Friv 100
Friv 8
Friv 2

GirlsGoGames Dressup said...

You want to relax after a stressful working hours. Refer to our website. Hope you get the most comfort.
Thanks for sharing !
GirlsGoGames
Kizi 100
Frvi

games2girls said...

You need to have time to take care of the kids active. Please visit our website and play exciting flash games.
Thanks you for sharing!
Games2Girls
Kizi 1000
Frin
Friv 3

Game Kizi said...

Thanks for sharing the information. It is very useful for my future. keep sharing. Can you play more games at: Friv1000 | Kizi4 | happy wheel