Marc,

Do all ports GC? (am thinking especially about file and tcp client and server ports)

I suppose that the fact that with-*-port closes the port on finalization cannot be relied on to always work, because what if there's an exception during the thunk's execution and it's handled in a way that never leaves control back, or the thread is thread-terminate!:d (could happen a dynamic-wind finalizer would not be trigged right)?

Mikael

2012/10/27 Marc Feeley <feeley@iro.umontreal.ca>

Le 2012-10-27 à 4:31 AM, Richard Prescott <rdprescott@gmail.com> a écrit :

> Good morning everybody,
>
> Forgive my ignorance, I am just learning.
>
> In order to embrace the "code is data" philosophy [1], I want to load and save data as s-expressions within files.  I figured the load using (read (open-input-file "data.scm")).
>
> How can I save?
>
> Thanks in advance.
>
> Richard

While it does work, your code does not close the input-port and relies on the garbage collector to do this, which is a bad idea (because you may run out of file descriptors before a garbage collection is triggered).  It is better to use call-with-input-file which closes the port.

Here are better save and restore functions:

;; File: save-restore.scm

(define (save-to-file filename obj)
  (call-with-output-file
    filename
    (lambda (port) (write obj port))))

(define (restore-from-file filename)
  (call-with-input-file
    filename
    (lambda (port) (read port))))

(save-to-file "data.txt" '#(1 2 3))

(pp (restore-from-file "data.txt"))


Marc

_______________________________________________
Gambit-list mailing list
Gambit-list@iro.umontreal.ca
https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list