Hi all,

I'm playing around with core.match and trying to extend it to some
custom types in terms of extending the IMatchLookup protocol on those.
But that's not quite what I need.  IMatchLookup only specifies how to
access some value out of my custom types, and in the `match' clauses
those values are compared by equality to the values specified there.
In my patterns, I want to have a :+type key with a value that's
basically an interface name given as a symbol.  The pattern should
match, if the class of the object implements that interface (or an
extended interface thereof) directly or indirectly.  So that would be my
ideal user interface:

--8<---------------cut here---------------start------------->8---
(match [obj]
  [{:+type 'Cat :mice 4}]   :1
  [{:+type 'Dog :cats 3}]   :2
  [{:+type 'Mammal}]        :3
  :else                     :nope)
--8<---------------cut here---------------end--------------->8---

For example, a Cat object with 7 mice would miss the first row but match
the third one, because Cat is a specialization of Mammal.

I can still achieve what I want by introducing an :+obj key returning
the actual object in favour of :+type and then use guards to do the
dispatch on type, but that puts the burden on the user.  `type-matcher'
is a custom function that gets some type specification and returns a
predicate that tests if a given object conforms to the type
specification.

--8<---------------cut here---------------start------------->8---
(match [node]
  [{:+obj (a :when (core/type-matcher node 'Cat)) :mice 4}]   :1
  [{:+obj (b :when (core/type-matcher node 'Dog)) :cats 3}]   :2
  [{:+obj (c :when (core/type-matcher node 'Mammal))}]        :3
  :else :nope)
--8<---------------cut here---------------end--------------->8---

Clearly, that's much worse than the idealized user interface above.  So
is there a way to do what I want?  Maybe even core.match could check, if
IMatchLookup/val-at* returned a function, and if so, simply apply it to
the value specified in the pattern as match test instead of the
hard-coded `='?

I have some more use-cases where I would be very benefitical to return
predicates for matching from val-at*.  For example, if some property of
my object is a collection, I'd very much like to have a key whose value
should be contained in the collection.

Thanks for any hints,
Tassilo

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to