[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: load in environment
From: |
Jon Wilson |
Subject: |
Re: load in environment |
Date: |
Fri, 06 Jul 2007 01:19:06 -0400 |
User-agent: |
Thunderbird 1.5.0.12 (X11/20070604) |
Stephen Compall wrote:
What other Guile state might you want to modify in the dynamic context
of a load, though?
Dynamic-wind looks like a quite good idea here. I'll probably use
that. Thanks.
Well, I'm writing a function to load up some data from a file and stick
it in a hash table. The data file looks something like this:
-- begin data file --
(item "foo" "bar is a metasyntactic variable" 1)
(item "baz" "barn is a unit of cross section" 2)
(item "frob" "nitz is more or less meaningless" 86)
-- end data file --
Then I call a function to load this data.
(define (load-data filename)
(let* ((my-table (make-hash-table))
(item (lambda (name text number)
(hash-set! my-table name (make-item text number))))
(m (make-module))
(real-current-module (current-module)))
(module-define! m 'item item)
(set-current-module m)
(load filename)
(set-current-module real-current-module)
my-table)))
[note: make-item is a record constructor.]
I certainly could just write code that reads in an expression from the
file, and sticks the data from it into the hash table. Probably safer
than using load (nothing is evaluated), and perhaps better for other
reasons as well. But this way is very pretty (I think). Additionally,
if I add (module-use! m (null-environment 5)) or some such, then the
user can generate the data programmatically, which sounds attractive
(there isn't currently a good use-case for this, but I can picture some
down the road perhaps).
I'm not sure if my-table is what you meant by "other guile state" that I
might want to modify, but it is definitely something visible to the rest
of the program which I want to modify by loading filename. I'm a little
bit mystified that I can modify my-table from inside (load filename) at
all, since my-table is certainly not visible to the code inside
filename. I guess that I can because my-table is wrapped up in the
closure of item. I'm not really clear on the interaction between
modules (especially set-current-module) and closures. A closure inside
of one module can apparently reference and modify things in quite
another module altogether. Maybe if it were not 1am, this would be more
obvious.
Regards,
Jon