Share your thoughts in the 2021 Clojure Community Survey!

Welcome! Please see the About page for a little more info on how this works.

+2 votes
in Protocols by
retagged by

Currently extend-protocol-defined functions look like this in the stacktrace:

at app.core$eval3547$fn__3548.invoke(core.clj:23)
at app.core$eval3522$fn__3523$G__3513__3532.invoke(core.clj:14)

Needless to say these aren't exactly descriptive. Not to mention the lines it refers to are where extend-protocol and defprotocol were called, not where the function definition is.

I'd hope for something like app.core/MyProtocol/IfaceImplemented/function_name_1234 with the line number where the "offending" piece of code is, not the definition.

I hacked at it a little locally and found that changing emit-method-builder, emit-impl and emit-hinted-impl a little made the stacktraces much more readable.

Hack diff:

diff --git a/src/clj/clojure/core_deftype.clj b/src/clj/clojure/core_deftype.clj
index 786f0d4b..73570dbf 100644
--- a/src/clj/clojure/core_deftype.clj
+++ b/src/clj/clojure/core_deftype.clj
@@ -586,9 +586,10 @@
 
 (defn- emit-method-builder [on-interface method on-method arglists extend-via-meta]
   (let [methodk (keyword method)
-        gthis (with-meta (gensym) {:tag 'clojure.lang.AFunction})
-        ginterf (gensym)]
-    `(fn [cache#]
+        gthis (with-meta (gensym (str method)) {:tag 'clojure.lang.AFunction})
+        ginterf (gensym)
+        giface-name (gensym (last (.split (name on-interface) "\\.")))]
+    `(fn ~giface-name [cache#]
        (let [~ginterf
              (fn
                ~@(map 
@@ -812,9 +813,21 @@
                    (:var proto)))))
     (-reset-methods (alter-var-root (:var proto) assoc-in [:impls atype] mmap))))
 
+(defn- iface-fn-name
+  ([p f]
+   (iface-fn-name "" p f))
+  ([c p f]
+   (let [c-name (cond
+                  (symbol? c)
+                    (last (.split (name c) "\\."))
+                  (nil? c)
+                    "nil"
+                  :else nil)]
+     (symbol (str p (when c-name (str "__" c-name)) (first f))))))
+
 (defn- emit-impl [[p fs]]
   [p (zipmap (map #(-> % first keyword) fs)
-             (map #(cons `fn (drop 1 %)) fs))])
+             (map #(cons `fn (cons (iface-fn-name p %) (drop 1 %))) fs))])
 
 (defn- emit-hinted-impl [c [p fs]]
   (let [hint (fn [specs]
@@ -826,7 +839,7 @@
                               body))
                       specs)))]
     [p (zipmap (map #(-> % first name keyword) fs)
-               (map #(cons `fn (hint (drop 1 %))) fs))]))
+               (map #(cons `fn (cons (iface-fn-name c p %) (hint (drop 1 %)))) fs))]))
 
 (defn- emit-extend-type [c specs]
   (let [impls (parse-impls specs)]

Logs after:

at app.core$eval3965$ProtocolName__ClassName__FnName__3966.invoke(core.clj:23)
at app.core$eval3940$ProtocolName3932__3941$__FnName3930__3950.invoke(core.clj:14)

2 Answers

+1 vote
by
0 votes
by

Legibility is good.

I dimly almost remember that Windows somehow limits the length of package+class names.

If the proposal involves lengthening class names, it could bring a risk of making certain Clojure programs no longer work in Windows.

by
You can name the functions using extend directly already, so that really shouldn't be an issue I believe
...