String representation of user data in Racket

I like how you can save the view in transparent structures:

(struct posn (x y)
        #:transparent)

> (posn 1 2)
(posn 1 2)

But is there a way to configure it? How is it in Python ?

+5
source share
1 answer

Look here for the prop:custom-writeproperty here . Here's a simple implementation:

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

Please note that this also works with no structures #:transparent.

+8
source

All Articles