Honor a deftype's custom Object/toString in .toString and str (jolt-rt6n)

A deftype with (Object (toString [_] s)) had its toString ignored: the generic
object-methods "toString" fired in dispatch-member before the record's own
method (the record isn't a tagged shim, so that guard passed), and str rendered
the #Type{...} data repr instead of routing through toString.

- dispatch-member: a record's own method (instance/reified/protocol) now wins
  over the generic object-methods table — so .toString/.equals/.hashCode on a
  record use the record's definitions; plain records still reach object-methods.
- str: add a late-bound record-tostring-cb (wired per-ctx by
  install-print-method-cb!, mirroring print-method-cb) that str-render-one
  consults for records — a deftype with a custom toString renders via it, plain
  records keep the data repr. pr-str is unchanged.

Needed by hiccup's RawString. Adds deftype-tostring-spec (.toString + str +
concatenation + a regression guard that record-less-toString keeps its repr).
This commit is contained in:
Yogthos 2026-06-15 10:22:09 -04:00
parent fb1ec25c69
commit a2c4fe317b
4 changed files with 62 additions and 4 deletions

View file

@ -0,0 +1,22 @@
# Specification: a deftype's custom Object/toString is honored by both .toString
# and str (jolt-rt6n). Before: object-methods' generic toString intercepted the
# record's .toString (the record isn't a tagged shim), and str rendered the
# #Type{...} repr instead of routing through toString. Needed by hiccup's
# RawString (a deftype with toString).
(use ../support/harness)
(defspec "deftype / custom toString"
[".toString uses the method" "\"hi\""
"(do (deftype Foo [s] Object (toString [_] s)) (.toString (->Foo \"hi\")))"]
["str uses the method" "\"hi\""
"(do (deftype Foo [s] Object (toString [_] s)) (str (->Foo \"hi\")))"]
["str concatenation uses it" "\"<hi>\""
"(do (deftype Foo [s] Object (toString [_] s)) (str \"<\" (->Foo \"hi\") \">\"))"]
["computed toString" "\"v=7\""
"(do (deftype Boxed [v] Object (toString [_] (str \"v=\" v))) (str (->Boxed 7)))"]
# a record WITHOUT a custom toString keeps the #Type{...} repr (regression guard)
["defrecord without toString keeps repr" "true"
"(do (defrecord Bar [x]) (boolean (re-find #\"Bar\" (str (->Bar 1)))))"]
# pr-str of a defrecord is unaffected (still the data repr)
["pr-str of a defrecord is the repr" "true"
"(do (defrecord Baz [x]) (boolean (re-find #\"\\{\" (pr-str (->Baz 1)))))"])