Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • My first realization that I really wasn't "thinking" Java was when I wanted a dispatch table whose keys were anonymous subs closing over values determined at runtime. I wound up creating a class hierarchy and used polymorphism to handle the dispatching. Instead of having all of my logically connected behavior in one place, I wound up spreading it across multiple classes which were created solely for the purpose of handling this dispatching. Being forced to shoehorn everything into "one-size-fits-all-OO-s

    • by jdavidb (1361) on 2008.02.21 11:34 (#61199) Homepage Journal

      There's actually a semi-decent way to handle some of that. You define an abstract class (or interface) with the method you want to be able to call. Then, you define anonymous inner subclasses of that class:

      public abstract DispatchInstance {
      public abstract void processIt();
      }

      class OtherClass {
      DispatchInstance[] table = new DispatchInstance[] {
        new DispatchInstance() {
          public void processIt() {
            int value = val1;
          }
        },
        new DispatchInstance() {
          public void processIt() {
            int value = val2;
          }
        },
        ...
      }

      Not sure if that would have fit the bill or not, but it's easier than making explicit subclasses for every single one of those!

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
      • Wow. Ugly as hell, but yet, much better than what I wrote. Reminds me of how some Java programmers argue that they don't need closures because they have anonymous inner classes. It's true, I suppose, but it's an awful lot of grunt work.

        As you can see, I'm not much of a Java hacker :)

        • Well, I just completed a class where I actually had to use this technique, and I'll testify that I definitely would've rather had something else. And you're right; it is ugly. In my case I don't think it was closures I was looking for, but I'm not sure what it was.

          The good news is I replaced it with something even more amazing.

          --
          J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers