String representation of custom data in Racket
Asked Answered
H

1

5

I like how you can retain representation in transparent structs:

(struct posn (x y)
        #:transparent)

> (posn 1 2)
(posn 1 2)

But is there a way to customize it? Like in Python?

Housewifery answered 12/8, 2011 at 14:1 Comment(0)
L
8

Check out the prop:custom-write property 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>

Note that this works with non-#:transparent structures as well.

Leopoldine answered 12/8, 2011 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.