extend-protocol当defrecord和protocol都在另一个命名空间中时



我试图使用扩展协议来扩展协议和来自不同名称空间的记录,但我得到以下错误:

Syntax error compiling at (src/http_client/client_mixed.clj:10:1).
No such var: either/Right

代码如下:

(ns http-client.client-mixed
(:require [cats.core :as m]
[cats.monad.either :as either]
[fmnoise.flow :as f]))
(extend-protocol f/Flow
either/Right
(?ok [this f] (f (m/extract this)))
(?err [this _] (m/extract this))
(?throw [this] (m/extract this))
either/Left
(?ok [this _] (m/extract this))
(?err [this f] (f (ex-info "Either.Left" (m/extract this))))
(?throw [this] (throw (ex-info "Either.Left" (m/extract this)))))

defrecord必须作为常规Java类导入。所以,这应该是正确的代码:

(ns http-client.client-mixed
(:require [cats.core :as m]
[cats.monad.either :as either]
[fmnoise.flow :as f])
(:import (cats.monad.either Left Right)))
(extend-protocol f/Flow
Right
(?ok [this f] (f (m/extract this)))
(?err [this _] (m/extract this))
(?throw [this] (m/extract this))
Left
(?ok [this _] (m/extract this))
(?err [this f] (f (ex-info "Either.Left" (m/extract this))))
(?throw [this] (throw (ex-info "Either.Left" (m/extract this)))))

最新更新