This construct is used to define an object with multiple
methods. This definition expression evaluates to an object, and the
param variable is defined to hold this object. Each "to"
with its following scope box is a method. No two methods may have the
both the same verb and number of arg-patterns (or
arity ). When this object is called, if the calling message does
not have the same verb and arity as any of the object's methods, a java.lang.NoSuchMethodException
is thrown.

If it does match a method, then the arguments are matched
against the arg-patterns. If this match fails, a ***to
be specified exception is thrown. It this succeeds, then, in the
context of the resulting variable definitions, the method-expression is
evaluated. The value of the method expression is the value of the call.

***Below is Really complicated explanation
of shorthands, longhands, and I didn't get it

In this classic
example, the outer object is written as a function, and the inner
object is written out explicitly.

***must invent better examples than
the predicate category thing. Need to start out with a simpler example,
then in intro to this thing, you need concrete example of 2 predicates
you want to use and make them conjunct, etc. Vector addition for points.***

Let's refer to a one argument function that returns a boolean
as a predicate. In other words, let's say that an object with a
one argument "run" method that returns a boolean is
a predicate. We can think of a predicate as like a category -- whether
the predicate's run method says true or false determines if the
argument is or isn't in the category. However, when thinking of categories,
it is natural to want to combine categories with the equivalent of intersection,
union, etc.

The simple predicate notion does not provide for this, and
if we required every predicate to support these new operations, we'd make
it too hard to define a new predicate. Instead, here's makeCategory
function, which, when given a predicate, gives back a category. Categories
act like the predicate they wrap, but also support composition with the
usual "&" (and),"|" (or), "^" (xor),
and "!" (not) operators.

***no prior discussion of how &
maps onto "and". I recommend using "and"
in the example, and on the way thru, mention in passing we could have
used "&" which maps onto "and".

Matching Messages

What if none of the methods match a call? Using the earlier
form, an exception will always get throws back to the caller, but sometimes
the callee would like to intervene. After the set of methods, an object
definition may optionally have a sequence of matchers. These are the
same matchers that appear in the switch statement, and you can validly
think of them as if they were enclosed in a

In other words, the verb and args from the incoming message
are turned into a two-element ConstList which is then pattern matched
against each of the patterns in sequence until a match succeeds. If
none succeed, a ***to be specified exception
is thrown. If one does match, the corresponding matcher-expression is
evaluated, and the value of the matcher-expression is the value of the
call.

One use of the flexibility is simply a more flexible
alternative to method definition. Even though an individual method definition
only accepts a fixed number of arguments, here's a version of our distance
function that can accept any number of arguments.

Although this example illustrates the power of matchers
in object definitions, it's poor style. For such examples, its usually
better to accept an EList as an argument.

***make sure in EList chapter you
have an example of using an EList to send a variable size bag of arguments
to a function

More interesting is the use of matchers for delegation.
In the above makeCategory code, the category objects respond to "and",
"or", "xor", and "not" themselves, but
pass the "run(specimen)" message through to the wrapped predicate.
If a predicate responds only to "run(specimen)", then a category
that wraps this predicate is like the predicate and more. However, it
a particular predicate object also responds to additional messages the
category doesn't know about, the above category code will hide this
part of the wrapped predicate.

Here is an alternative category implementation that uses
match to delegate all messages it doesn't understand to the wrapped
predicate.

"E" is a primitive service which is
always in scope. Use "E.call(pred,verb,args)"
to call an object with a computed message, when you don't statically
know the verb or the individual arguments. x.max(y) is equivalent
to E.call(x,"max",[y]).

***need to define a thunk as an
expression to evaluate, perhaps a primality test

The postponed argument given to LazyMaker
is a no-argument function (ie, a thunk), and held by the resulting
lazy object, represents the potential (or postponed) calculation.
The actual calculation happens when the thunk is called. In lazy evaluation,
once the calculation is performed, it's result is remembered and reused.