Skip to content

Instantly share code, notes, and snippets.

@belmarca
Created April 26, 2020 01:05
Show Gist options
  • Save belmarca/d225ac1cca13f46d01528fead40200c3 to your computer and use it in GitHub Desktop.
Save belmarca/d225ac1cca13f46d01528fead40200c3 to your computer and use it in GitHub Desktop.
Hooking gambit's ##wr.
;; cb.ss
(defstruct ugly-struct (a b c))
(defstruct wonderful-struct (a b c))
(defmethod {:wr wonderful-struct}
(lambda (self we obj)
(with ((wonderful-struct a b c) self)
(##wr-str we "πŸ¦„<")
(##wr-str we " 🌈🌈🌈 ")
(##wr-str we (string-append
"a: " (number->string a)
" b: " (number->string a)
" c: " (number->string a)))
(##wr-str we " 🌈🌈🌈 ")
(##wr-str we ">πŸ¦„"))))
(def us (ugly-struct 1 2 3))
(def ws (wonderful-struct 1 2 3))
(def (gerbil-##wr we obj)
(let (wr (find-method (object-type obj) ':wr))
(if wr
(wr obj we obj)
(##default-wr we obj))))
(##wr-set! gerbil-##wr)
;; > us
;; #<ugly-struct #2>
;; > ws
;; πŸ¦„< 🌈🌈🌈 a: 1 b: 1 c: 1 🌈🌈🌈 >πŸ¦„πŸ¦„< 🌈🌈🌈 a: 1 b: 1 c: 1 🌈🌈🌈 >πŸ¦„
;; >
@fare
Copy link

fare commented Jan 13, 2021

Looks like in current Gerbil, the method has arguments (self we) not (self we obj)β€”indeed the obj was redundant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment