Skip to content

Instantly share code, notes, and snippets.

@capfredf

capfredf/ge2.rkt Secret

Last active February 24, 2021 21:03
Show Gist options
  • Save capfredf/e2375151ad3c2c19209daa5f63db643d to your computer and use it in GitHub Desktop.
Save capfredf/e2375151ad3c2c19209daa5f63db643d to your computer and use it in GitHub Desktop.
(define-generics printable
(gen-print printable [port])
(gen-port-print port printable)
(gen-print* printable [port] #:width width #:height [height]))
(define-struct bool (v)
#:methods gen:printable
[(define/generic super-print gen-print)
(define (gen-print n [port (current-output-port)])
(fprintf port "Bool: ~a" (bool-v n)))
(define (gen-port-print port n)
(gen-print n port)
(super-print n port))
(define (gen-print* n [port (current-output-port)]
#:width w #:height [h 0])
(fprintf port "Bool (~ax~a): ~a" w h (bool-v n)))])
(define-struct num (v)
#:methods gen:printable
[(define/generic super-print gen-print)
(define (gen-print n [port (current-output-port)])
(fprintf port "Num: ~a" (num-v n)))
(define (gen-port-print port n)
;; (gen-print (bool (< (num-v n) 10)) port) ;; error, this is a call to gen-print on L22
(super-print (bool (< (num-v n) 10)) port))
(define (gen-print* n [port (current-output-port)]
#:width w #:height [h 0])
(fprintf port "Num (~ax~a): ~a" w h (num-v n)))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment