-
Notifications
You must be signed in to change notification settings - Fork 16
/
custom-write.rkt
31 lines (25 loc) · 979 Bytes
/
custom-write.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#lang racket/base
(require racket/contract/base)
(provide
(contract-out
[custom-write-mode/c flat-contract?]
[custom-write-function/c chaperone-contract?]
[make-named-object-custom-write
(->* (symbol?) (#:name-getter (-> any/c (or/c symbol? #false)))
custom-write-function/c)]))
;@------------------------------------------------------------------------------
(define custom-write-mode/c (or/c boolean? 0 1))
(define custom-write-function/c
(-> any/c output-port? custom-write-mode/c void?))
(define (make-named-object-custom-write type-name
#:name-getter [get-name object-name])
(define type-part (string-append "#<" (symbol->string type-name)))
(λ (this out mode)
(parameterize ([current-output-port out])
(write-string type-part)
(define name (get-name this))
(when name
(write-string ":")
(write-string (symbol->string name)))
(write-string ">"))
(void)))