The following does not work, for obvious reasons.
(defprotocol Monoid
(mappend [a b])
(mzero []))
mzero
has zero arguments, and zero argument methods are not allowed (or do not make sense) in protocols. In Haskell or Scala, where the dispatch is type-based rather than value-based, this is not a problem.
What would be the correct way to conceptualize and write Monoid
protocol in Clojure?
mappend
would be in the protocol, but the equivalent ofmzero
would be a function that creates an implementation of that protocol (and lives in a namespace - module - with the implementation). what i am missing, from the above, is how something likemzero
can be implied by context separately from a particular implementation. – andrew cooke May 26 '12 at 18:51