SPUG: Unidentified Flying Objects

Fred Morris m3047 at inwa.net
Tue Jan 10 11:57:27 PST 2006

At 2:21 PM 1/10/06, Adrian Hands wrote:
>I wrote:
>> [...]something is. But sometimes you don't yet in the course of executing a
>> behavior it becomes obvious what it is.
>so, you do a cast, but from Flyer to Plane (or Flyer to Bird), but not
>Bird to Plane.
>  Flyer ufo = new Bird();
>  ( (Plane) ufo ).on_a_typical_day();
>  // you'll get a ClassCastException at runtime.

No. It is not a cast. It is a zen thing: it is not a bird wearing an
airplane's clothes, it changes from a bird into a plane. With enough
template hacking maybe somebody can do it in C++; I'd be afraid.

Again, it is not a cast (which to me implies temporarily cloaking an object
in the appearance of another, typically to fool a compiler and not to
directly alter execution). It is something, however, which is conceivable
in a semi-interpreted language such as Perl where class is simply an
attribute which is bound to a reference (reference as a general concept, as
opposed to a narrow definition such as a container for a memory address);
in fact I would have been far more surprised if it hadn't worked.

There is an old quote somewhere that C++ is to C as lung cancer is to lung.
In this case the adage might be Perl is to OO as C is to assembler. (If
comparing C to assembler seems silly to you, you need only look at the C
compilers released for free-running microprocessors with a whopping 8K of
flash and 256 bytes of RAM... or reflect on the processor which C was
originally developed for.)  So the question: "can I do this in another
implementation environment" == "how processor-specific is this".

>When is it a good idea to change a bird into a plane?

And the answer is when it becomes a plane.

Is that the right question? When is it a good idea to eat lunch?

(Your truncation to "...something is" is serendipitous.) It is not the
object which changes, but our perception of it. The code is neither the
bird nor the plane, but our perception of it. It's a zen thing.

I appreciate your asking, but the question I would ask is: when is it worth
doing something brilliant as opposed to upholding rigid conventions or
belief systems which in and of themselves may fail your "good idea" test?
Is it a good idea to construct your own bird, or is it better to let them
hatch from eggs? Planes, of course, don't hatch from eggs; yet the ontology
of the development of mechanized flight certainly reveals its inspiration
from the natural world.

I would proffer that doing something like this requires developing some
appropriate style guidelines so that you don't for example, change a bird
into a rock: something which Perl would also allow (it enforces no
discernible constraints on what an object can be changed into, for instance
must be a subclass of the same base class.. the potential for mayhem is I
hope obvious, it is to me).

More information about the spug-list mailing list