Cliff Hacks Things.

Saturday, May 27, 2006

The Mongoose syntax evolves, with a comparison to Java

I'm tinkering on a new syntax for Mongoose, which incorporates the type system I've spoken of in the past.

As an example, let's look at a simple Java class, and how we would implement the same class in Mongoose.

This class is called Filter, and comes from some dataflow-style processing code of mine. Basically, this code constructs a pipeline of producers and consumers, each of which does some processing or conversion of the data. Producers will finish when they have completed processing and will produce no further output. This finished status propagates forward through the other stages, and is used at the terminal stage(s) to trigger report generation or aggregation.

The Filter can sit between some number of Producers and some number of Consumers. It tests its input, and if it matches some condition, passes it on.

First, the groundwork. These interfaces implement the Producer/Consumer pattern, with some additional signalling for when processing at each stage has completed.

Pretty straightforward. Now, I have a lot of different implementations of Producer, some of which need to inherit from existing classes (generally for legacy reasons). Thus, I package up the basic Producer functionality in a ProducerSupport class, to which Producers can delegate most operations. It has two utility methods, emit and finish, that the owning Producer can call to control its operation.

Now, the Mongoose version. I'll go through two iterations, the first closer to the Java version.

A couple quick notes on this syntax:

For generic types, the formal parameters and invocation type parameters are given in [square brackets].

Type annotations are given in <angle brackets>. They follow the argument or slot that they describe. Methods may have an additional annotation describing the return type; if present, it follows the argument list and is separated by the return symbol ^.

If this reminds you of Strongtalk, you're a geek.

Yes, there are two types of brackets in these definitions. Square brackets surround blocks of code; curly brackets surround structural definitions. This may seem annoying, but there's actually a good reason for this (which unfortunately doesn't come up in this example).

The interesting bits, starting from the top:Behavior and state in Mongoose classes are separated into two aspects, class and instance. This is equivalent to the static/member divide in languages like Java or C++. The main difference, in Mongoose, is that the two can actually be defined separately, though I haven't done that here.

The method #for: is implemented as a constructor, which is analogous to Java and C++ again, but a bit more rigorously defined and flexible. A constructor, in Mongoose, is a method that is invoked on a class, but executes on an object. More importantly, a constructor is invoked like any other method on the class; callers don't know they're invoking a constructor, and you can change the implementation — for example, to return a singleton instance, or delegate to another class — without changing the callers.

Notice that the owner argument to #for: lacks a type annotation. This is an implementation shortcut; because the argument is being directly stuffed into the slot _owner, which has a type annotation, the type can be inferred. (Callers would see the type annotation when inspecting the class.)

The rest is pretty simple. The words constant and private following slot and method definitions (after the semicolon) are attributes. constant says that a slot, once initialized, will never change; this also allows you to omit the type annotation. As with final fields in Java, the slot must have an explicit initializer, or be set in a constructor. private says that a method is only available to other code within the class or subclasses thereof.

Most of this you've seen above; we have a basic constructor and some instance behavior and state. However, a few points here merit further explanation.

The constructor takes the predicate, which performs the actual filtration. It's typed as a Block, which is a standard Mongoose class describing a block of code (as in Smalltalk).

Notice, in the instance aspect, that we're missing most of the code from the Java implementation. The key is the second line in the aspect:implements Producer[E] via _ps.

This is called a via, and allows us to explicitly delegate all messages in the Producer protocol to another object — in this case, the object in the slot _ps. Variations on vias allow you to delegate only a portion of a protocol, or a few named messages, but those aren't applicable here.

In our case, the contents of _ps are constant, but we might choose to switch out the object; any messages in the Producer protocol would automatically be forwarded to the new object.

So, we've reimplemented the Java version without some tedious, boilerplate code. How would we use such a class?

Simply passing in a Block that performs our test is a lot simpler, not to mention more concise, than Java's anonymous class mechanism.

So, we've eliminated a chunk of boilerplate code, and made the class easier to use. However, we can do better. Delegation is all well and good, but in this case, we're delegating to a single object that was specifically designed for delegation.

This pattern smells like Java. There's a better way to do this in Mongoose.

ProducerSupport, in Java, was designed as a reusable unit to provide Producer behavior through delegation. Here, we've reimplemented ProducerSupport as a trait — a reusable chunk of state and behavior that classes can include. The code in ProducerSupport will run within the class that includes it, so we've eliminated the concept of "owner" and the accompanying code — now, the ProducerSupport is the owner, and vice versa.

I've highlighted the changes. By including the ProducerSupport trait, we've automatically gained all its behavior — so we no longer need to keep track of a ProducerSupport instance, or delegate using the via. Filter also automatically implements the protocols exposed by its included traits — in this case, Producer.

Filter, at this point, has degenerated into a basic Adapter pattern, converting the language's Block construct for use in the producer-consumer architecture. The class is nearly trivial.

I've tried to demonstrate the utility of the following Mongoose features:

Linguistic support for delegation through via.

Blocks.

Traits.

This new syntax is a work in progress; all the concepts described here work in my current Mongoose implementation, but the compiler is evolving as I work. Any comments, suggestions, or questions are welcome.

About

A blog for my tech rants. My rants, suggestions, ideas, and general pedantry here are representative solely of this geek-at-arms, and not my employer, affiliated civic service organizations, deceased ancestors, or people whose names rhyme with my own.

About Me

Name:Cliff Biffle

Location:Mountain View, California, United States

I'm a Java developer for a large unspecified company, aficionado of programming languages, and general Mac bigot.
Contrary to what you may have read on the intarweb, I am not, in fact, a "Linux stalwart."