Frogs and Forests

April 28th, 2004 1:47 PM

Encouraged by a rumor heard at last night’s Boston.pm meeting that my forests and frogs made the cut, I’ve finally slogged through the whole of Apocalypse 12. Sure enough, my RFC 254 gets a full thousand words of coverage, being solved in a most excellent way.

The initial problem was the inability to subclass two classes that maintain a hasa relationship. The simplistic example was a forest class and a frog class, with the former containing the latter. Now consider needing to subclassing the general forest with a Japanese forest, and the general frog with one that けろけろs instead of ribbiting. Since the general forest class has a constructor that calls the Frog class explicitly:

$self->{'frog'}  = new Frog;

this seemed like a problem that needed fixing.

A12 solves this problem by aliasing global classes as inner classes, and referencing the classes through their inner definition. By redefining the inner class in a subclass, we can override the constructor call with the new inner class’ constructor.

From page 17 of the Apocalypse (with Unicode added for proper Japanese ribbits, and some extraneous bits left out):

class Forest {
  our class Frog {
    method speak () { say "ribbit ribbit"; }
  }
  has Frog $.frog;
  method new ($class) {
    my Frog $frog .= new;  # MAGIC
    return $class.bless( frog => $frog );
  }
}

class Forest::Japanese is Forest {
  our class Frog is Forest::Frog {
    method speak () { say "けろけろ"; }
  }
}

The difference is on the line marked “MAGIC”. Because Frog was mentioned in a method, and the invocant was of type Forest::Japanese rather than of type Forest, the word “Frog” figured out that it was supposed to mean a Forest::Japanese::Frog rather than a Forest::Frog. The name was “virtual”. So we ended up creating a forest with a frog of the appropriate type, even though it might not have occurred to the writer of Forest that a subclass would override the meaning of Frog.

I’m sold.

#Perl
Next: Welcome
Previous: Memory

Comments

i’m getting really curious to see this new beast known as perl 6 when it’s finally released. and here i thought that perl had the kitchen sink in version 5 =)

Posted by: gary on April 28th, 2004 10:47 PM

Well, don’t hold your breath. It’s been almost four years since the ball started rolling on Perl 6, and we’re still a long ways off from having a Perl 6 “release.” Not that there hasn’t been forward movement; Parrot has turned from an April Fool’s joke into a fantastic virtual machine with exceptional features (a multi-platform jit, garbage collection, an assembler that handles register spilling, native calling interface, …). But even a development version of a Perl 6 compiler will take some time yet. In the mean time, the apocalypses just keep making the language design more and more slick, and the Ponie project looks enticing as a good way to bridge from Perl 5 to Perl 6 :)

Posted by: kasei on April 28th, 2004 11:27 PM

Heh. Losers. [via Simon]

Posted by: kasei on April 29th, 2004 1:54 PM

げろげろ not けろけろ I dunno what kinda frogs you heard… but the one’s I heard definitely voiced that げ。

ハハハ ^_^

Posted by: sara on May 2nd, 2004 9:38 AM