Re: defmethod question....
On Sun, 27 Dec 2009 08:59:53 -0800 (PST) Jason Wolfe jawo...@berkeley.edu wrote: That doesn't seem to be possible - I can't find a function that accepts a PersistentStructMap and returns the symbol passed to struct so it knows the keylist to use for the arguments. Nor could I find the magic incantation to let me use those symbol names with derive, which would let me use keywords for them. I got something working by having the dispatch function use sets of keys from the maps, but that seems fragile. Writing defFOO and FOO macros that attached the appropriate metadata to each struct seem possible, but sorta ugly. I believe this is the current way to do it. For each struct type I intend to use in this way, I write a (defn make-A [a b c] (with-meta (struct A a b c) {:type ::A})) function and use this to construct all of my struct instances. To bad. I did think of a nice way to do this with current constructs, but it doesn't work now :-(. However, not only does it solve my problem, it might be useful elsewhere, so I'll describe it in hopes that it might get further consideration. Basically, the idea is to allow destructured argument lists in an overloaded defn, treating them as failed if some of the assignment in the argument list aren't made. So you could solve this problem with something like: (defstruct foo :a :b :c) (defstruct bar :c :d :e) (defn struct-dispatch ([{_ :a _ :b _ :c}] ::foo) ([{_ :c _ :d _ :e}] ::bar)) (defmulti struct-method struct-dispatch) (defmethod struct-method ::foo ; work on struct foo ) (defmethod struct-method ::bar ; work on struct bar ) Other supported destructurings would also be possible. In the extreme case, Oz-style unification would be really cool, but that could also be really expensive. mike -- Mike Meyer m...@mired.org http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more information. O ascii ribbon campaign - stop html mail - www.asciiribbon.org -- 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
defmethod question....
First, an aside: I was pleasantly surprised to find that the defmulti dispatch function could itself be polymorphic. Well, surprised isn't the right word - I half expected it to work. But it's always nice when new tools follow POLA. Which leads to a question where Clojure doesn't seem to follow POLA. I want to have a multimethod dispatch on structure types. Specifically, if I define two structures: (defstruct A :a :b :c) (defstruct B :b :d :e) I want a multimethod that uses different methods for A B. I'd *like* to be able to write them like: (defmethod meth A ...) (defmethod meth B ...) That doesn't seem to be possible - I can't find a function that accepts a PersistentStructMap and returns the symbol passed to struct so it knows the keylist to use for the arguments. Nor could I find the magic incantation to let me use those symbol names with derive, which would let me use keywords for them. I got something working by having the dispatch function use sets of keys from the maps, but that seems fragile. Writing defFOO and FOO macros that attached the appropriate metadata to each struct seem possible, but sorta ugly. Anyone got a suggestion here? Thanks, mike -- Mike Meyer m...@mired.org http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more information. O ascii ribbon campaign - stop html mail - www.asciiribbon.org -- 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
Re: defmethod question....
That doesn't seem to be possible - I can't find a function that accepts a PersistentStructMap and returns the symbol passed to struct so it knows the keylist to use for the arguments. Nor could I find the magic incantation to let me use those symbol names with derive, which would let me use keywords for them. I got something working by having the dispatch function use sets of keys from the maps, but that seems fragile. Writing defFOO and FOO macros that attached the appropriate metadata to each struct seem possible, but sorta ugly. I believe this is the current way to do it. For each struct type I intend to use in this way, I write a (defn make-A [a b c] (with-meta (struct A a b c) {:type ::A})) function and use this to construct all of my struct instances. Future versions of Clojure will have datatypes [1], which improve on structmaps in many ways (typed fields, real type fields for multimethod dispatch, ...), as well as protocols [2], which will replace many instances of multimethods. [1] http://www.assembla.com/wiki/show/clojure/Datatypes [2] http://www.assembla.com/wiki/show/clojure/Protocols They are there in the new branch to be tried out, although I think details are still subject to change. -Jason -- 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