Pattern Matching (RFE)

Imam Tashdid ul Alam <[email protected]> Tue, 23 Aug 2005 21:24:00 -0700 (PDT)
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
(RFE posted)

my proposed match keyword didn't really click, so I am
trying something similar.
for completeness, here is my proposed syntax for match
(which was originally meant to replace value
dispatch):

match(anObject) {
case null: // if the object can have null value
case anotherObject: // checks if they point to the
same object, useful for enumerations
case SomeClass c: // this is an instanceof
checking, c is the same object as an instance of
SomeClass
case SomecClass c, c.someProperty() =3D=3D
somethingElse: // an even exhaustive check
case _: // the default case
}

I suggested that there could be a tuple syntax for it,
to reduce complicated if-else nestings.

here is an alternative proposition. this one basically
provides a way for an object to have a useful tuple
representation for pattern matching.

in class Complex, for example:
match Complex(double real) {
if(this.imaginary !=3D 0)
throw new PatternMatchFailedException();
return (this.real);
}

match Complex(double real, double imaginary) =3D
(this.real, this.imaginary);

to be used like this:
double magnitude(Complex c);
magnitude(Complex(double real)) =3D Math.abs(real);
magnitude(Complex(0, double imaginary) =3D
Math.abs(imaginary);
magnitude(Complex(double real, double imaginary)) =3D
Math.sqrt(real*real + imaginary*imaginary);

or
realPart(Complex(double real, _)) =3D real;

note that matching this way, much of the getXXX()
methods can be omitted by giving the accessed property
a name. the compiler can decide which elements of the
tuple are actually needed and store it in variables
for
later use. I think this construct goes very well with
the Nice philosophy in general.

regards (I'm ashamed, but cannot help thinking of ways
to reduce the pains of a Java programmer)

Imam Tashdid ul Alam

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practic=
es
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & Q=
A
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf