[plt-scheme] Objects, classes and match
On Fri, Oct 3, 2008 at 6:16 AM, Sam TH <samth at ccs.neu.edu> wrote:
> On Fri, Oct 3, 2008 at 8:12 AM, Dave Gurnell <d.j.gurnell at gmail.com> wrote:
>> Hi all,
>>
>> Is there any way to use match on an object? I can't see anything in the docs
>> (although I may be missing something obvious). Something like:
>>
>> (match (new my-class%)
>> [(struct my-class% (a b c))
>> ...])
>>
>> I don't mind setting up a custom match-expander. I suppose I could convert
>> the object to a vector and match on that. That doesn't seem like a
>> particularly efficient solution, though.
>
> There isn't anything in match at the moment for this. If I was
> implementing this, I'd probably want this to do something like:
>
> (class [a x] [b y] [c z]) => (app (lambda (e) (list (send e a) (send e
> b) (send e c))) (list x y z))
>
> maybe using `get-field' instead of `send'.
>
> If you have other ideas for the API, I'm happy to hear them. I
> haven't found anything I particularly like, and that's why there's
> nothing in `match' at the moment.
I wrote an experimental matcher for doing this recently and found that
it's nice to match using both get-field and send (because some classes,
like point% in scheme/gui iirc) don't work with just get-field.
The syntax that I had worked out looked something like:
object-pat := (object class-expr [pat] obj-sub-pat ...)
obj-sub-pat := (field-id pat)
| ((message arg ...) pat)
The "danger" of matching on a send though is that the send should
probably not mutate the object.
Cheers,
Sam