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

オープン
#1,024 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
26

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

babashka/sci のほかの issue

babashka/sci の issue をすべて見る

似ている issue

Clojure の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。