简体   繁体   English

Clojure(脚本)静态协议?

[英]Clojure(Script) Static Protocols?

The ClojureScript library Om Next makes use of a static protocols . ClojureScript库Om Next使用静态协议 I have never seen this before and am wondering if it's an Om specific concept, or an actual part of the language. 我以前从未见过这个,我想知道它是否是Om特定的概念,或者是语言的实际部分。 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). (将其键入REPL不会产生任何错误,因此我认为它不是Om特定的)。

deftype is a low level feature of the language, it's how Clojure is built. deftype是该语言的低级功能,它是Clojure的构建方式。 At it's core it's just a java Class constructor, it sets fields and methods. 在它的核心它只是一个java类构造函数,它设置字段和方法。 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. java中的static是一个关键字,表示声明的方法是Class的成员,而不是Class的实例,因此通过继承全局可用于实例。 defui is a macro much like deftype but instead of Classes it's a constructor for javascript Object Prototypes which are analogous. defui是一个非常像deftype的宏,但它不是Classes,它是javascript Object Prototypes的构造函数,它们是类似的。 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. 可以实例化对象原型,因此要使所有实例均匀可用的行为需要具有静态字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM