gpt4 book ai didi

clojure - Clojure 协议(protocol)源自哪种语言的哪些特性?

转载 作者:行者123 更新时间:2023-12-02 12:55:52 25 4
gpt4 key购买 nike

Clojure 协议(protocol)源自哪种语言的哪些特性?

注意:这不是一个关于“如何使用协议(protocol)”的问题

这是一个关于这个想法的灵感来源的问题。

特别是,我想要的是得到类似的东西:

  • XYZ + ABC 启发的 Clojure 协议(protocol)

然后,我可以去阅读人们使用 XYZ 和 ABC 来了解语言功能的说明。

谢谢!

最佳答案

如果您了解 Java,Clojure 协议(protocol)与 Java 接口(interface)类似 - 但更好。 Clojure website说了有关协议(protocol)的内容(该页面上有更多有关协议(protocol)的背景信息和基本原理):

  • Provide a high-performance, dynamic polymorphism construct as an alternative to interfaces
  • Support the best parts of interfaces
    • specification only, no implementation
    • a single type can implement multiple protocols
  • While avoiding some of the drawbacks
    • Which interfaces are implemented is a design-time choice of the type author, cannot be extended later (although interface injection might eventually address this)
    • implementing an interface creates an isa/instanceof type relationship and hierarchy
  • Avoid the 'expression problem' by allowing independent extension of the set of types, protocols, and implementations of protocols on types, by different parties
    • do so without wrappers/adapters
  • Support the 90% case of multimethods (single dispatch on type) while providing higher-level abstraction/organization

此外,您可能会发现 Solving the Expression Problem with Clojure 1.2有趣的读物:

Clojure expert Stuart Sierra introduces you to new features in Clojure 1.2 that solve the Expression Problem, a classic programming dilemma. Protocols let you extend preexisting types to new methods, and datatypes let you extend preexisting methods to new types — all without changing the existing code. You'll also see how Java™ interfaces and classes can interact with Clojure protocols and datatypes.

关于clojure - Clojure 协议(protocol)源自哪种语言的哪些特性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11496521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com