Re: Re: pattern matching on fields of the argument

Daniel Bonniot <[email protected]> Fri, 06 Aug 2004 10:33:11 +0200
Newsgroups gmane.comp.lang.nice.devel
Message-ID <[email protected]>
>>foo(Apply a) = if (a.e1 is Lambda && a.e1.id.equals("foo") && a.e2 == null)
>>It's still a one liner (introducing 'is' for 'instanceof'),
> 
> 
> Aha!  Now I see where you're going.  Yes, this will work quite well.
> The 'is' operator is the key to making everything work smoothly and
> concisely.

I did not think you would find it good enough!

The clear thing is that boolean conditions are useful in general, and `is` is 
simple to implement. So if that serves your needs for now, it makes sense to 
implement this first.

(for `is`, I think it should be possible to accept it in this form without 
making it a keyword, since it can also be use as an identifier -- thinks 
'InputStream is').


> Three other (independent) suggestions:
> 
>   - It would be nice if 'is' were a non-alphanumeric token in order to
>     eliminate the extra whitespace.  Perhaps
> 
>              a.e1~Lambda

The drawback of this is that it is cryptic (read, perlish). That is, it's 
probably not self-explaining. So I would be reluctant to make it part of the 
language. On the other hand, you should be able to use operator-overloding to 
use any operator for this meaning, given that type expressions are becoming 
first-class (in the CVS version fully-qualified types are, and I think I will 
generalize it to others too).

In the long term, I think it would still be a good idea to have deep patterns, 
which should alleviate the need for extra-short instanceof test.

>   - It would also be cool if there were a very short form of
>     'if (x != null && x.equals(y))'

It's already there:
   x.equals(y)
works with possibly-null x value. Here is the implementation in nice.lang:

<T> boolean equals(?T a, ?T b)
{
   if (a != null && b != null)
     return a.equals(b);
   else
     return a==b;  // true iff both null
}

>   - Finally -- and I hesitate to suggest this because I'm not sure
>     it's a good idea -- you could combine these operations into a
>     generalized pattern-match operation
> 
>          a.e1~~Lambda(e1===x,e2~Integer)

If we had deep patterns, wouldn't this become overkill?

>     This is a pretty ambitious goal; obviously I'm working on the
>     simpler approach first, but I think something similar to this is
>     worth discussing.

Yes, we can keep this for further discussion, based on the experience we will 
collect.

Good luck!

Daniel

PS: Adam, would you subscribe to nice-devel?


-------------------------------------------------------
This SF.Net email is sponsored by OSTG. Have you noticed the changes on
Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now,
one more big change to announce. We are now OSTG- Open Source Technology
Group. Come see the changes on the new OSTG site. www.ostg.com