Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General syntax of multimethods

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.

like image 869
Andrea Avatar asked Mar 31 '12 22:03

Andrea


2 Answers

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
like image 115
Jeremy Avatar answered Nov 20 '22 02:11

Jeremy


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).

like image 40
uvtc Avatar answered Nov 20 '22 02:11

uvtc