[plt-scheme] How to write and read structs?

From: Phil Rand (philrand at gmail.com)
Date: Sat Sep 29 21:23:59 EDT 2007

I'd like to write a list of structs to a file, then read it back, and then
access the structs as structs, not as vectors.  What am I missing?

Do I need to write a vector to struct converter?

Here's an example:

;; test.scm

(define-struct point (x y) #f)
(with-output-to-file "test.txt"
  (lambda () (write (list (make-point 0 1) (make-point 1 0))))

(define (points)
  (let ((stuff '()))
    (with-input-from-file "test.txt"
      (lambda ()
        (set! stuff (read))))

(car (points))
--> #(struct:point 0 1)

(point? (car (points)))
--> #f

Phil Rand
philrand at pobox.com

Posted on the users mailing list.