std/protocol

模块 zio.protocol — Protocol system (on ZOS generic functions) · 2 个符号

Clojure-style protocols: (defprotocol Name (method [args])) and (extend-type Type Protocol (method [this & args] body)).

Status: Experimental. Both macros are runnable; methods dispatch through the ZOS generic function machinery.

概览

defprotocol

(defmacro defprotocol [name & sigs] …)

defprotocol — 将每个方法签名声明为同名的 ZOS 泛型函数。

(defprotocol name (method [args])...) 展开为一串 (defgeneric method args),方法分发依赖 ZOS 泛型函数机制。 实验性功能。

extend-type

(defmacro extend-type [type-name protocol-name & method-impls] …)

extend-type — 为 type-name 实现 protocol 的各个方法。

每个方法实现 (method [this & args] body...) 展开为 (defmethod method [(this type-name) ...] body...),即在第一个 参数的类型上特化,其余参数保持不变。