Gimme a break!

[*Update: It has been revealed that my code sample below exposes a bug that has been fixed in the Groovy-1.1 branch. So in theory my example should work but it requires 1.1 (which I’ve not downloaded or tried yet) to work.]

After reading the remainder of the thread I was referring to earlier I noticed there are several issues with my approach. (I also learned that it could be accomplished with closures alone.) One important got’cha would be the lack of break support. Ok, you got me! The hack get’s uglier. Here’s what I came up with after spending another two-three minutes on the topic:

class BreakUnlessException extends RuntimeException {

class Conditions {
   public static void unless(Object obj, boolean cond, Closure work) {
      if(! cond) {
	try { }
	catch(BreakUnlessException e) { }
   public static void breakUnless(Object obj) {
      throw new BreakUnlessException()

use(Conditions) {
   unless( 1+1 > 2 ) { println 'Math is working' }
   unless( 5 > 1 ) { println 'Conditions do not work.' }
   unless( 1+1 > 2 ) { 
      println 'we should break after this line'
      println 'we should not hit this line.'
   println 'We end up here!'

Guess what? It doesn’t work like I thought it would! If you hold your head at a 57 degree angle while aligning your body such that the angle of your glare is exactly parallel with the Earth’s axis, squint both eyes keeping the right eye slightly more squinted than the left and increase your font size by one or two points (Firefox users can hit Ctrl+Plus once or twice to achieve this) you might see where my intention is to intercept the flow of logic through the injection of an exception (yes it’s yucky but what else you gonna do?) in the middle of the third unless block. You might also guess (as I did) that the exception would be caught in the above Category method since that is the caller of the Closure doing the work. However, Groovy seems to think that the try/catch that I’ve so aptly wrapped around the work is irrelevant. Groovy seeks to deliver my Exception elsewhere in some magic portal where many exceptions go to bypass there would-be handlers only to appear directly in your production logic. I would spend a little more time correcting the issue, but hey, I’m lazy. I’ll leave it as an exercise for the reader. (That’s you!) If you hang in my spot I need the info you got. Do us all a favor and drop it like it’s hot…

(Newcomers to my site should not be confused/misled by the above signoff. The phrase “drop it like it’s hot” is not intended to encourage clumsiness with any objects of high temperatures. Instead the idea is for you to feel entertained while you are encouraged to leave textual feedback in the below entry form. Consider the entire final two sentences. When read aloud during office hours in an open cubicle one would be perceived as having incredible hip-hop lyrical skills by random onlookers, potentially resulting in an eventual recording contract with a company like Def Jam or Jive. )

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s