Search code examples
clojureclojurescriptom

Clojure(Script) Static Protocols?


The ClojureScript library Om Next makes use of a static protocols. I have never seen this before and am wondering if it's an Om specific concept, or an actual part of the language. Here is a simplified down code snippet:

(deftype type
  static IProtocol
  (some-method [this] "val"))

What exactly does this do? (Typing it into a REPL doesn't yield any errors, so I'm led to believe it's not Om specific).


Solution

  • deftype is a low level feature of the language, it's how Clojure is built. At it's core it's just a java Class constructor, it sets fields and methods. A static in java is a keyword that indicates that the methods declared are members of the Class, not of the instance of the Class, thus globally available to instances through inheritance. defui is a macro much like deftype but instead of Classes it's a constructor for javascript Object Prototypes which are analogous. The main difference is that it doesn't take fields just methods. Object Prototypes can be instantiated, so for behavior to be uniformly available to all instances they need to have static fields.