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

时间:2011-08-12 14:01:43

标签: string scheme racket representation

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

(struct posn (x y)
        #:transparent)

> (posn 1 2)
(posn 1 2)

但有没有办法定制它?就像Python

一样

1 个答案:

答案 0 :(得分:8)

查看prop:custom-write属性here。这是一个简单的实现:

(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>

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

相关问题