Racket中自定义数据的字符串表示

Vla*_*hev 5 string scheme representation racket

我喜欢你如何在透明结构中保留表示:

(struct posn (x y)
        #:transparent)

> (posn 1 2)
(posn 1 2)
Run Code Online (Sandbox Code Playgroud)

但有没有办法定制它?像在Python中一样?

Sam*_*adt 8

这里查看prop:custom-write酒店.这是一个简单的实现:

(struct pr (x y)
  #:transparent
  #:property prop:custom-write (? (v p w?)
                                 (fprintf p "<~a,~a>" (pr-x v) (pr-y v))))

> (pr 1 2)
<1,2>
Run Code Online (Sandbox Code Playgroud)

请注意,这也适用于非#:transparent结构.