groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "KARR, DAVID" <dk0...@att.com>
Subject RE: Curious that Groovy case can match values that are not equal to the candidate
Date Sat, 08 Aug 2015 16:44:22 GMT
Could you elaborate on that last point?  As we’re talking about the special semantics of
the candidate value, are you saying the candidate value itself can be a closure?

From: Guillaume Laforge [mailto:glaforge@gmail.com]
Sent: Saturday, August 08, 2015 9:10 AM
To: users@groovy.incubator.apache.org
Subject: Re: Curious that Groovy case can match values that are not equal to the candidate


You can use closures for arbitrary conditions (the return value is evaluated via Groovy truth),
or regexes so it checks the toString of the value, etc.

Pretty powerful!
Le 8 août 2015 17:09, "Owen Rubel" <orubel@gmail.com<mailto:orubel@gmail.com>>
a écrit :
Fascinating. Had no idea. Learn something new every day :)

Owen Rubel
415-971-0976
orubel@gmail.com<mailto:orubel@gmail.com>

On Fri, Aug 7, 2015 at 10:21 PM, Guillaume Laforge <glaforge@gmail.com<mailto:glaforge@gmail.com>>
wrote:

Hi David,

Groovy 's switch is a bit special and his beyond Java' s.
Please have a look at the documentation about it.
When a list is passed, it checks if the element is contained within.

Guillaume
Le 7 août 2015 23:07, "KARR, DAVID" <dk068x@att.com<mailto:dk068x@att.com>> a
écrit :
Reading REGINA, I find this detail of Groovy semantics very curious:
----------------------
def myList = ['a', 'b', 'c']
switch ('c') {
case myList: assert true;break;
default: assert false;break;
}
--------------

In all the languages I'm aware of with some sort of "switch/case" construct, you can always
assume that if the "case" matches, then the "case" value "is equal to" the switch candidate.
 This is the first time I've seen this not be the case.

I certainly understand what Groovy is doing here, and I appreciate the power of it, it's just
a bit surprising.

Mime
View raw message