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

12 comments:

Kit Plummer said...

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

Anonymous said...

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

The Speculator 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

Unknown 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

Unknown 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

hammad 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

john smith said...

The FCC requires cell phone manufacturers to ensure that their phones comply with these objective limits for safe exposure. Security Equipment Wholesalers

Foogle Packers and Movers said...

Thanks for information provide list of verified post articles compare & save money and select the best # Foogle Packers And Movers

packers and movers Delhi
packers and movers Pune
packers and movers Gurgaon
packers and movers Mumbai
packers and movers Bangalore
packers and movers Hyderabad
packers and movers Chennai
http://www.fooglepackers.com

WorldNews said...

Thanks for sharing

giay luoi nam

https://danongonline.com.vn/tag/giay-luoi-nam

Bếp Royal said...

Quý khách đang tìm hiểu bếp điện từ loại nào tốt hoặc bếp điện từ giá bao
nhiêu?

Chúng tôi xin tư vấn nên tham khảo các thương hiệu bếp điện từ tại Showroom bếp
Royal!

Tại thiết bị nhà bếp Royal chuyên các sản phẩm bếp điện từ nhập khẩu Đức và bếp
điện từ nhập khẩu Tây Ban Nha.

Bếp Royal sẽ tư vấn kỹ chức năng, công dụng, thông số kỹ thuật và đơn giá của
từng loại bếp điện từ.

Thế mạnh của bếp Royal là dịch vụ lắp đặt bếp điện từ uy tín chuyên nghiệp, bảo
hành tại nhà lên đến 3 năm, “chúng tôi luôn luôn cố gắng hết sức để đem lại sự
hài lòng cho quý khách“.

Ngoài ra Bếp Royal luôn có chương trình “khuyến mại bếp điện từ giá rẻ” và “khuyến
mại đổi bếp” để phục vụ nhu cầu của quý khách hàng.

Bếp điện
từ Fagor
,
bếp điện
từ Cata
,
bếp
điện từ Dmestik
,
bếp điện
từ Chefs
,
bếp điện
từ Teka
,
bếp điện
từ Faster
,
bếp
điện từ Napoliz

meibo wood watch said...

A good quality cute iphone xr cases can prevent falling damages to your phone and a iPhone X cases for girls can make your ix even more eye catching.
protective iphone xs max cases for girls are our recommendation to all girls handling a a protective iPhone xs ma.
cute phone 8 plus cases for girls , iphone 7 plus cases for girls and girly iphone 6s plus case
accessories are available in our store too! One of which is long necklaces for women. Make your style stunning by wearing new modern designed necklace jewelries.