General syntax of multimethods
Asked Answered
E

2

9

I apologize if the question is trivial, but some googling is not leading me anywhere. What is the general syntax of defmulti and defmethod? I can write simple multimethods, but I am not sure where I can put the docstring, pre and post conditions, metadata and so on.

I am actually interested in ClojureScript more than in Clojure, so if there are differences between the two, please tell me.

Egocentrism answered 31/3, 2012 at 22:22 Comment(0)
B
7

In a REPL you can use the doc function to get the function arguments and (most of the time) an explanation of the options. As for ClojureScript, these two functions are macros, which means they are expanded at compile time and should behave exactly as they do in regular Clojure. That is, as long as ClojureScript can handle the code the macro generates.

user=> (doc defmulti)
-------------------------
clojure.core/defmulti
([name docstring? attr-map? dispatch-fn & options])
Macro
  Creates a new multimethod with the associated dispatch function.
  The docstring and attribute-map are optional.

  Options are key-value pairs and may be one of:
    :default    the default dispatch value, defaults to :default
    :hierarchy  the isa? hierarchy to use for dispatching
                defaults to the global hierarchy
nil
user=> (doc defmethod)
-------------------------
clojure.core/defmethod
([multifn dispatch-val & fn-tail])
Macro
  Creates and installs a new method of multimethod associated with dispatch-value. 
nil
Bran answered 31/3, 2012 at 22:55 Comment(0)
F
4

At Clojuredocs: defmulti, defmethod.

If you don't find the examples there detailed enough, you might consider adding your own (once you've gotten all your questions answered).

Fontanez answered 1/4, 2012 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.