[plt-scheme] How to write and read structs? Follow-up and fun with modules
Well, that turned out to be more of struggle than I expected.
I wanted to wrap my serializable structs inside a module, but I had
trouble getting the requires and provides working. My original plan
was to import the serialize library from my module, and re-export it
along with the structs. A client program would simply import my
module to access everything. I never got it to work that way, trying
quite a few combinations. Either I'd get complaints about multiple
definitions, or the client program wouldn't have what it needed.
What finally worked was to import the serialize library in the clients
as well as the module, and to use the (provide (struct ...)) form in
the module to export my structs. I was surprised the latter worked,
since I used define-serializable-struct rather than define-struct.
Here's my test code. Improvements welcome.
;; test-mod.ss
(module test-mod mzscheme
(require (lib "serialize.ss"))
(provide (struct point (x y)))
(define-serializable-struct point (x y)))
;; test.scm
(require (lib "serialize.ss")
"test-mod.ss")
(with-output-to-file "test.txt"
(lambda () (write
(list
(serialize (make-point 0 1))
(serialize (make-point 1 0)))))
'replace)
(define (points)
(let ((serialized '()))
(with-input-from-file "test.txt"
(lambda ()
(set! serialized (read))))
(map deserialize serialized)))
(map point? (points))
The final (map point? (points)) returns (#t #t), so I claim success.
By the way, this was DrScheme 371, on Mac OSX 10.4.10.
On 9/30/07, Phil Rand <philrand at gmail.com> wrote:
> Thanks!
>
> The serialize.ss library will work just fine for me. I think I've
> come across it before, but was scared off by its complexity arising
> from its generality. Of course that generality is exactly what I was
> asking for, even though I was only thinking about my very simple
> application.
>
> Phil
>
> On 9/30/07, Richard Cleis <rcleis at mac.com> wrote:
> > In the help desk, you can "Find docs for: serialize" to lead you to
> > Chapter 43 of PLT MzLib: Libraries Manual.
> >
> > rac
> >
> > On Sep 29, 2007, at 7:23 PM, Phil Rand wrote:
> >
> > > 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))))
> > > 'replace)
> > >
> > > (define (points)
> > > (let ((stuff '()))
> > > (with-input-from-file "test.txt"
> > > (lambda ()
> > > (set! stuff (read))))
> > > stuff))
> > >
> > > (car (points))
> > > --> #(struct:point 0 1)
> > >
> > > (point? (car (points)))
> > > --> #f
> > >
> > > --
> > > Phil Rand
> > > philrand at pobox.com
> > > _________________________________________________
> > > For list-related administrative tasks:
> > > http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> >
> >
>
>
> --
> Phil Rand
> philrand at pobox.com
>
--
Phil Rand
philrand at pobox.com