5

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

(struct posn (x y)
        #:transparent)

> (posn 1 2)
(posn 1 2)

但是有没有办法自定义它?就像在Python中一样?

4

1 回答 1

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>

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

于 2011-08-12T15:21:08.697 回答