This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
It's driving me insane! I'm pretty new to groovy and programming in general, and I'm writing a simulation model using RePast, which is based on Groovy. I created a map 'schedmap' which has other maps' names as its values:
I also created an empty map called 'schedmaps1'. What follows is troubleshooting code:
I get the following output for lines 2-5 above:
So, for lines 2 and 3, I get the map printed, which is what I want. The problem is that I can't explicitly state that I want schedmaps1, because the map I need will vary from iteration to iteration, so I need to call the correct map from schedmap, based on another object's variables (line 4) or at least allow the spcID (from another class) to dictate the correct map (a la line 5). The funny thing is, I was able to call a map from a map in other code, but I am completely unable to get it to work now. I guess the ultimate question is, in lines 4 and 5, how do I get Groovy to see 'schedmaps1' as a reference instead of as final output?
Many thanks in advance!
Joined: Aug 17, 2012
Hmm, it looks like I got it. If I get the current object and call the maps with respect to that object (i.e., scheduler.schedmaps1) it works. That doesn't make sense, but I'll run with it!
A map of maps to values is usually a "smell" that indicates you're trying too hard to write flexible code while sacrificing clarity of purpose. Your short description alone takes a couple of re-readings to just begin to follow. I bet if you read the code you wrote a few weeks from now, you'd be scratching your head. If not two weeks, then two months from now. The point is, the logic is convoluted and that's never a good thing.