extend-protocol doesn't work on deftype-fn backed types

未关闭
#1,024 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
clojure
领域
backend

调研方向

从 sci.impl.types/type-impl 开始,跟踪 issue 中描述的 SciTypeInstance 和 ICustomType 路径。运行提供的 extend-protocol 复现,然后验证由 deftype-fn 支持的自定义实例是否与其特定类型匹配,同时确保内联协议实现继续正常工作。

由索引模型根据 Issue 内容生成。

描述

extend-protocol doesn't work on deftype-fn backed types

Problem

When a deftype is backed by a custom type via :deftype-fn (e.g. SciMap in babashka), extend-protocol on that type doesn't work.

The root cause is that type-impl returns :sci.impl.protocols/reified for ICustomType instances (rather than the specific type symbol like user.MyCache), so the defmethod registered by extend-protocol for the specific type never matches.

Reproduction

(defprotocol Extra (extra [this]))
(extend-protocol Extra
  MyCustomType
  (extra [this] :it-works))
(extra (->MyCustomType ...))
;; => No implementation of method: :extra of protocol: #'user/Extra found for: :sci.impl.protocols/reified

Analysis

In sci.impl.types/type-impl:

  1. SciTypeInstance is checked first → returns specific type symbol (e.g. user.Foo) — extend-protocol works
  2. ICustomType is checked second → returns :sci.impl.protocols/reified — extend-protocol doesn't match

For the standard SciType path, inline protocol impls are registered as defmethods keyed on the specific type symbol. extend-protocol also registers defmethods for that symbol, so both work.

For custom types (via :deftype-fn), inline protocol impls go through the :reified defmethod which delegates to ICustomType.getMethods(). But extend-protocol registers a defmethod for the specific type symbol, which never fires because type-impl returns :reified.

Possible fix

Make custom type instances also implement SciTypeInstance (or otherwise return a specific type symbol from type-impl), then register a per-type defmethod that delegates to getMethods(). This would let both inline impls and extend-protocol coexist.

This would also open the door to unifying the standard SciType path and the custom type path, since both would use methods-map + per-type defmethod delegation.

主要语言
Clojure
星标
1.4k
派生
109
平均合并
1 分钟
30 天内合并 PR
26

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

babashka/sci 的其他 Issue

查看 babashka/sci 的全部 Issue

相似的 Issue

更多 Clojure Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。