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

Reply via email to