core: print-method is a real multimethod (jolt-g1r); records print canonically

print-method/print-dup are now multimethods in the io tier with Clojure's
exact dispatch ((:type meta) keyword, else type — core.clj 3693). On jolt the
dispatch value for a record is its quoted full-name symbol, since class names
aren't values here.

Records used to pr-str as the raw janet table; the renderer's record branch
now prints Clojure's #ns.Type{:k v} syntax, and first consults a callback the
api wires up after the overlay loads — so a user defmethod on a record type
fires everywhere: top level, nested in collections, through pr/prn/pr-str.
Builtin overrides (a :number method) fire only on direct print-method calls;
pr keeps the native fast path (documented divergence).

java.io.Writer arrives as a shim beside the StringReader/StringBuilder ones:
a :jolt/writer tagged value with write/append/flush/toString, a StringWriter
ctor, and a sink variant the renderer callback uses.

Two latent host bugs fixed on the way: the interpreted syntax-quote splice
blew up on ~@nil (an interpreted macro's empty & rest binds nil — first tier
user of defmulti found it; d-realize now treats nil as the empty seq), and
(print-method x nil) now throws like the JVM instead of returning nil.

10 spec rows; bench dead even (sandwich run); greeter green on a fresh
binary.
This commit is contained in:
Yogthos 2026-06-11 18:10:11 -04:00
parent eff8cb99a5
commit 1e4a0a6d53
8 changed files with 131 additions and 9 deletions

View file

@ -67,3 +67,31 @@
["pr writes no newline" "\"\\\\a\"" "(with-out-str (pr \\a))"]
["print nil arg" "\"\"" "(with-out-str (print nil))"]
["prn keyword" "\":k\\n\"" "(with-out-str (prn :k))"])
# print-method is a real multimethod (jolt-g1r): canonical dispatch on
# (:type meta) keyword else (type x); records print as #ns.Type{...} by
# default, and a user (defmethod print-method 'ns.Type ...) overrides record
# rendering everywhere — top level AND nested, through pr/prn/pr-str — via
# the host renderer's callback. Builtin overrides apply only on direct
# print-method calls (documented divergence; pr keeps the native fast path).
(defspec "io / print-method multimethod"
["records print canonically" "\"#user.Pt{:x 1, :y 2}\""
"(do (defrecord Pt [x y]) (pr-str (->Pt 1 2)))"]
["records nested in colls" "\"[#user.Pt{:x 1, :y 2}]\""
"(do (defrecord Pt [x y]) (pr-str [(->Pt 1 2)]))"]
["defmethod overrides a record, top level" "\"<3,4>\""
"(do (defrecord Pt [x y]) (defmethod print-method (quote user.Pt) [r w] (.write w (str \"<\" (:x r) \",\" (:y r) \">\"))) (pr-str (->Pt 3 4)))"]
["defmethod fires nested in a map" "\"{:p <5,6>}\""
"(do (defrecord Pt [x y]) (defmethod print-method (quote user.Pt) [r w] (.write w (str \"<\" (:x r) \",\" (:y r) \">\"))) (pr-str {:p (->Pt 5 6)}))"]
["defmethod fires through prn" "\"[<1,2>]\\n\""
"(do (defrecord Pt [x y]) (defmethod print-method (quote user.Pt) [r w] (.write w (str \"<\" (:x r) \",\" (:y r) \">\"))) (with-out-str (prn [(->Pt 1 2)])))"]
["direct call uses :default" "\"42\""
"(let [w (StringWriter.)] (print-method 42 w) (.toString w))"]
["direct builtin override" "\"#42#\""
"(do (defmethod print-method :number [n w] (.write w (str \"#\" n \"#\"))) (let [w (StringWriter.)] (print-method 42 w) (.toString w)))"]
["print-dup routes to print-method" "\"[1 2]\""
"(let [w (StringWriter.)] (print-dup [1 2] w) (.toString w))"]
["StringWriter accumulates" "\"ab\""
"(let [w (StringWriter.)] (.write w \"a\") (.append w \\b) (.toString w))"]
["methods table inspectable" "true"
"(do (defrecord Pt [x y]) (defmethod print-method (quote user.Pt) [r w] r) (contains? (methods print-method) (quote user.Pt)))"])

View file

@ -122,8 +122,8 @@
["proxy-super throws" :throws "(proxy-super count [1])"]
["re-groups throws" :throws "(re-groups (re-matcher #\"a\" \"b\"))"]
["re-matcher builds" "false" "(nil? (re-matcher #\"a\" \"abc\"))"]
["print-dup" "nil" "(print-dup 1 nil)"]
["print-method" "nil" "(print-method 1 nil)"]
["print-dup nil writer throws" :throws "(print-dup 1 nil)"]
["print-method nil writer throws" :throws "(print-method 1 nil)"]
["uri? string" "false" "(uri? \"http://x\")"]
["uri? nil" "false" "(uri? nil)"]
["definterface defines" "true" "(var? (definterface IFoo (foo [x])))"]