Dennis Haupt <d.haup...@gmail.com> writes:

> i was taking a look at multimethods:
> (defmulti fac int)
> (defmethod fac 1 [_] 1)
> (defmethod fac :default [n] (*' n (fac (dec n))))
>
> this works
>
> however, this also works:
>
> (defmulti fac print)
> (defmethod fac 1 [_] 1)
> (defmethod fac :default [n] (*' n (fac (dec n))))

This definately does not work when I try it:

     user> (defmulti fac print)
     #<Var@6427854d: #<MultiFn clojure.lang.MultiFn@4bddef9a>>
     nil
     user> (defmethod fac 1 [_] 1)
     #<MultiFn clojure.lang.MultiFn@4bddef9a>
     nil
     user> (fac 1)
     IllegalArgumentException No method in multimethod 'fac' for dispatch 
value: null  clojure.lang.MultiFn.getFn (MultiFn.java:160)
     1

The second arg to defmulti's job is to decide which method to call. The
print function always produces nil, so you would need a defmethod for
dispatch value nil to use print as a dispatch function:

    user> (defmethod fac nil [_] 2)
    #<MultiFn clojure.lang.MultiFn@4bddef9a>
    nil
    user> (fac 1)
    12
    nil

Notice the "12": the "1" is from print and the "2" is the value the
method produced.

Are you trying to print the dispatch values so you can see them for
tracing or something? If so, you could try something like:

    user> (defn inspect [& stuff]
            (println "inspect: " stuff)
            (first stuff))
    #<Var@5b1413a8:
      #<user$eval336$inspect__337 user$eval336$inspect__337@68903261>>
    nil
    user> (inspect 1)
    inspect:  (1)
    1
    nil
    user> (defmulti fac2 inspect)
    nil
    nil
    user> (defmethod fac2 1 [_] 1)
    #<MultiFn clojure.lang.MultiFn@e2df60d>
    nil
    user> (fac2 1)
    inspect:  (1)
    1
    nil
    user>


-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to