|
124 | 124 | (defn ^:private standard-scitype-path |
125 | 125 | "Standard SciType path for deftype — protocol-only implementations." |
126 | 126 | [ctx form rec-type record-name factory-fn-sym fields field-set |
127 | | - protocol-impls raw-protocol-impls error-hint] |
| 127 | + protocol-impls error-hint] |
128 | 128 | (let [protocol-impls |
129 | 129 | (mapcat |
130 | | - (fn [[protocol-name & impls] expr] |
| 130 | + (fn [[protocol-name & impls]] |
131 | 131 | (let [impls (group-by first impls) |
132 | 132 | protocol (@utils/eval-resolve-state ctx (:bindings ctx) protocol-name) |
133 | 133 | _ (when-not protocol |
134 | 134 | (utils/throw-error-with-location |
135 | 135 | (str "Protocol not found: " protocol-name) |
136 | | - expr)) |
137 | | - _ (assert-no-jvm-interface protocol protocol-name expr error-hint) |
| 136 | + form)) |
| 137 | + _ (assert-no-jvm-interface protocol protocol-name form error-hint) |
138 | 138 | protocol (if (utils/var? protocol) @protocol protocol) |
139 | 139 | protocol-ns (:ns protocol) |
140 | 140 | pns (cond protocol-ns (str (types/getName protocol-ns)) |
|
176 | 176 | field-set))) |
177 | 177 | `(defmethod ~(fq-meth-name method-name) ~rec-type ~@bodies)))) |
178 | 178 | impls))) |
179 | | - protocol-impls |
180 | | - raw-protocol-impls)] |
| 179 | + protocol-impls)] |
181 | 180 | (emit-deftype rec-type record-name factory-fn-sym |
182 | 181 | `(defn ~factory-fn-sym [& args#] |
183 | 182 | (sci.impl.deftype/->type-impl '~rec-type ~rec-type (var ~record-name) (zipmap ~(list 'quote fields) args#))) |
|
186 | 185 | (defn deftype-macro |
187 | 186 | "Macro expansion for deftype. Emits a deftype* form like JVM Clojure. |
188 | 187 | Protocol names are collected into the :implements vector, methods follow." |
189 | | - [[_fname & _ :as _form] _ record-name fields & raw-protocol-impls] |
| 188 | + [[_fname] _ record-name fields & raw-protocol-impls] |
190 | 189 | (let [ns-name (utils/current-ns-name) |
191 | 190 | tagged-name (symbol (str ns-name) (str record-name)) |
192 | 191 | class-name (symbol (str (munge ns-name) "." record-name)) |
193 | 192 | protocol-impls (utils/split-when symbol? raw-protocol-impls) |
194 | 193 | interfaces (mapv first protocol-impls) |
195 | | - method-counts (mapv #(count (rest %)) protocol-impls) |
196 | 194 | methods (mapcat rest protocol-impls)] |
197 | 195 | (list* 'deftype* tagged-name class-name fields |
198 | | - :implements (with-meta interfaces {:method-counts method-counts}) |
| 196 | + :implements (with-meta interfaces {:protocol-impls protocol-impls}) |
199 | 197 | methods))) |
200 | 198 |
|
201 | 199 | (defn analyze-deftype* |
|
207 | 205 | rec-type class-name |
208 | 206 | factory-fn-str (str "->" record-name) |
209 | 207 | factory-fn-sym (symbol factory-fn-str) |
210 | | - ;; Reconstruct protocol-impls grouping from method-counts metadata. |
211 | | - method-counts (:method-counts (meta interfaces)) |
212 | | - protocol-impls |
213 | | - (let [all-methods (vec methods)] |
214 | | - (loop [ifaces (seq interfaces) |
215 | | - counts (seq method-counts) |
216 | | - offset 0 |
217 | | - result []] |
218 | | - (if ifaces |
219 | | - (let [cnt (first counts) |
220 | | - impls (subvec all-methods offset (+ offset cnt))] |
221 | | - (recur (next ifaces) (next counts) (+ offset cnt) |
222 | | - (conj result (into [(first ifaces)] impls)))) |
223 | | - result))) |
| 208 | + protocol-impls (:protocol-impls (meta interfaces)) |
224 | 209 | field-set (set fields) |
225 | 210 | result |
226 | 211 | #?(:clj |
|
281 | 266 | :fields (hash-map ~@field-entries) |
282 | 267 | :protocols ~protocols-form})))) |
283 | 268 | (standard-scitype-path ctx form rec-type record-name factory-fn-sym |
284 | | - fields field-set protocol-impls protocol-impls error-hint))) |
| 269 | + fields field-set protocol-impls error-hint))) |
285 | 270 | :cljs |
286 | 271 | (let [protocol-impls |
287 | 272 | (mapcat |
288 | | - (fn [[protocol-name & impls] expr] |
| 273 | + (fn [[protocol-name & impls]] |
289 | 274 | (let [impls (group-by first impls) |
290 | 275 | protocol (@utils/eval-resolve-state ctx (:bindings ctx) protocol-name) |
291 | 276 | protocol (or protocol |
|
296 | 281 | _ (when-not protocol |
297 | 282 | (utils/throw-error-with-location |
298 | 283 | (str "Protocol not found: " protocol-name) |
299 | | - expr)) |
| 284 | + form)) |
300 | 285 | protocol (if (utils/var? protocol) @protocol protocol) |
301 | 286 | protocol-var (:var protocol) |
302 | 287 | _ (when protocol-var |
|
346 | 331 | field-set))) |
347 | 332 | `(defmethod ~(fq-meth-name method-name) ~rec-type ~@bodies))))) |
348 | 333 | impls))) |
349 | | - protocol-impls |
350 | 334 | protocol-impls)] |
351 | 335 | (emit-deftype rec-type record-name factory-fn-sym |
352 | 336 | `(defn ~factory-fn-sym [& args#] |
|
0 commit comments