On Jul 28, 2012, at 7:02 AM, Ben Smith-Mannschott wrote: > On Fri, Jul 27, 2012 at 9:06 PM, Vinzent <ru.vinz...@gmail.com> wrote: >> robert-hooke actualy doesn't work with multimethods afaik. You can try my >> new library (https://github.com/dnaumov/hooks), but it's alpha (no docs yet, >> sorry). > > (defmulti foo* (fn [args] ...) ...) > (defmethod foo* :x [args]...) > (defmethod foo* :y [args] ...) > > (defn foo [args] > (foo* args)) > > Only foo calls foo*. Everyone else calls foo. Apply hooks to foo. > > http://en.wikipedia.org/wiki/Fundamental_theorem_of_software_engineering > > ;-) > > // Ben
robert.hooke works fine with multimethods: user=> (defmulti foo class) nil user=> (defmethod foo :default [x] (str x)) #<MultiFn clojure.lang.MultiFn@7539f0bb> user=> (require '[robert.hooke :refer (add-hook)]) nil user=> (add-hook #'foo (fn [f & [x]] (str "K: " (f x)))) (#<user$eval3072$fn__3074 user$eval3072$fn__3074@534b58c>) user=> (foo 42) "K: 42" More interesting still would be the ability to add hooks to particular methods. `defmethod` doesn't define a new var, so that's not generally possible, but you can work around it by defining functions and tying them to multimethods in separate operations: user=> (defmulti twice class) #'user/twice user=> (defn twice-n [n] (* n n)) #'user/twice-n user=> (defn twice-s [s] (str s s)) #'user/twice-s user=> (.addMethod twice Number #'twice-n) #<MultiFn clojure.lang.MultiFn@6a04919b> user=> (.addMethod twice String #'twice-s) #<MultiFn clojure.lang.MultiFn@6a04919b> user=> (twice 5) 25 user=> (twice "hi") "hihi" user=> (add-hook #'twice-n (fn [f & [n]] (f (dec n)))) (#<user$eval3044$fn__3046 user$eval3044$fn__3046@fa328aa>) user=> (twice 5) 16 These sorts of situations makes me want for an add-method to go along with remove-method and get-method, just to avoid the .addMethod interop form. - Chas -- 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