On Mar 30, 2013, at 12:00 AM, George Oliver wrote:
>
>
> On Friday, March 29, 2013 6:19:19 PM UTC-7, JvJ wrote:
> Is it possible to invoke a particular multimethod and bypass the dispatch
> function?
>
> For instance, suppose that I have a multimethod with a dispatch value of
> ::foo, and it's a really complex method.
>
> Now, I want all cases where the dispatch function returns nil to use the same
> multimethod. Is there a way I can intercept the nil and
> pass it directly on to the ::foo multimethod?
>
> You could use the default multimethod to catch nil, and then call the multi
> with ::foo.
That will only work if the dispatch fn is identity or similar.
If you control the dispatch fn, then you don't need to "intercept nil" -- just
returning ::foo from the dispatch fn when it might otherwise return nil will
get you to the method you want.
If you *don't* control the dispatch fn, but you're providing the methods for
:foo and nil, then you can use `get-method` to obtain the method associated
with a particular dispatch value:
=> (defmulti foo :bar)
#'user/foo
=> (defmethod foo :baz
[x]
(str x))
#<MultiFn clojure.lang.MultiFn@6194a872>
=> (defmethod foo :default
[x]
((get-method foo :baz) x))
#<MultiFn clojure.lang.MultiFn@6194a872>
=> (foo {:bar :baz})
"{:bar :baz}"
=> (foo {:some :other :value :here})
"{:some :other, :value :here}"
Of course, if the functionality you need is available as a separate top-level
fn, and you can avoid the extra effective dispatch through the multimethod, all
the better.
Cheers,
- Chas
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.