Is there any way to control how a struct is printed?
For example if I have a transparent struct containing an image:
(struct photo (label image-data) #:transparent)
But I don't want to print the image-data
field.
~n or ~% prints a newline character (which is equivalent to \n in a literal format string)
Programmer-Defined Datatypes in The Racket Guide introduces structure types via struct. A structure type is a record datatype composing a number of fields. A structure, an instance of a structure type, is a first-class value that contains a value for each field of the structure type.
I want to extend Ben's answer a bit. You can also combine gen:custom-write
with make-constructor-style-printer
to make the struct printing significantly easier. This function handles the differences between printing, writing, quote depth, and output port for you.
Extending his example gives:
#lang racket
(require pict
racket/struct)
(struct photo (label image-data)
#:transparent
#:methods gen:custom-write
[(define write-proc
(make-constructor-style-printer
(lambda (obj) 'photo)
(lambda (obj) (list (photo-label obj)))))])
(displayln (photo "fish" (standard-fish 100 100)))
;; Prints #<photo: fish>
(println (photo "fish" (standard-fish 100 100)))
;; Prints (photo "fish")
Now write
, display
, and print
all work as you would expect
Yes! Use the gen:custom-write
generic interface.
#lang racket
(require pict)
(struct photo (label image-data)
#:transparent
#:methods gen:custom-write
[(define (write-proc photo-val output-port output-mode)
(fprintf output-port "#<photo:~a>" (photo-label photo-val)))])
(photo "fish" (standard-fish 100 100))
;; Prints "#<photo:fish>"
The first argument to write-proc
is the struct to be printed.
The second argument is the port to print to.
The third argument suggests how the context wants the value printed, see the docs:
http://docs.racket-lang.org/reference/Printer_Extension.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._gen~3acustom-write%29%29
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With