[ nice-Feature Requests-1264897 ] Pattern Matching

"SourceForge.net" <[email protected]> Sat, 20 Aug 2005 07:45:21 -0700
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
Feature Requests item #1264897, was opened at 2005-08-21 00:45
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=362788&aid=1264897&group_id=12788

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
Status: Open
Priority: 5
Submitted By: Imam Tashdid ul Alam (uchchwhash)
Assigned to: Nobody/Anonymous (nobody)
Summary: Pattern Matching

Initial Comment:
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() ==
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 != 0)
        throw new PatternMatchFailedException();
    return (this.real);
}

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

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

or
realPart(Complex(double real, _)) = 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


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=362788&aid=1264897&group_id=12788


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