The answer to your question at the end, by the way, is that you're not
var-quoting the x. 'defn attaches metadata to the variable object you
define, not the function object itself. (meta x) is the metadata of
the _value_ of x, which is just the function with no metadata. To get
the variable's metadata, you need to refer to the variable itself, by
using #'x, equivalent to (var x). ^#'x, or (meta (var x)), should
return {:a 1}.

With my second example, I didn't mean that I was defining a new
defmethod or anything. I meant that the Clojure core's defmethod does
not accept any metadata, unlike defn. Now I see that it's possible to
attach metadata to the method's symbol—but that as far as I can tell
is unretrievable. Ah well, this might become possible in the future,
but for now I'll just put my tests in another file. :)

On Nov 22, 3:04 pm, "Michael Wood" <[EMAIL PROTECTED]> wrote:
> Hi
>
>
>
> On Sat, Nov 22, 2008 at 7:32 AM, samppi <[EMAIL PROTECTED]> wrote:
>
> > I'm trying to unit-test a mutli-function's methods without resorting
> > to a separate test file. I can do this:
>
> >  (defn foo
> >    ([x] (+ x 2))
> >    {:test (fn [] (= (foo 3) 4))})
>
> > ...but how do I do something like this?
>
> >  ; Does not work
> >  (defmethod foo :mapping
> >    ([x] (assoc x :a 5))
> >    {:test (fn [] (= (foo {:a 3 :b 2}) {:a 5 :b 2}))})
>
> > I can't find a way to attach metadata to methods. I can't find a
> > function that creates a standalone method that I can define a variable
> > with. Is it currently possible to somehow do this another way?
>
> Perhaps this thread will help with the metadata part of the question:
>
> http://groups.google.com/group/clojure/browse_thread/thread/f19ff07f8...
>
> It also seems to me like there's something wrong with your defmethod,
> although I've only just started trying to figure out multimethods.
>
> I think you actually need something like this:
>
> (defmulti foo :dispatch)
> (defmethod foo :mapping [x] (assoc x :a 5))
>
> and then you need to call it like:
>
> (foo {:dispatch :mapping :a 3 :b 2})
>
> With the metadata, I think it should be like this:
>
> (defmethod #^{:test (fn [] (= (foo {:dispatch :mapping :a 3 :b 2})
> {:dispatch :mapping :a 5 :b 2}))} foo :mapping [x] (assoc x :a 5))
>
> Although I think it makes more sense to attach the metadata to the
> defmulti than to the defmethod.
>
> I may be completely wrong about the above, since I've only just
> started looking at metadata and multimethods, but I thought I'd try to
> give you an answer seeing as nobody else has yet :)
>
> I do seem to be having some trouble with metadata, though (using Clojure 
> r1121):
>
> user=> (def #^{:a 1} x 'test)
> #'user/x
> user=> x
> test
> user=> (meta x)
> nil
> user=> (meta (with-meta x {:a 1}))
> {:a 1}
> user=>
>
> Shouldn't (meta x) return {:a 1}?
>
> I see that this works:
>
> (def x (with-meta 'test {:a 1}))
>
> although it seems that would attach the metadata to 'test instead of to x.
>
> (def #^{:a 1} x) (meta 'x) also returns nil.
>
> What am I missing?
>
> --
> Michael Wood <[EMAIL PROTECTED]>
--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to