Here's a library which exports a hashtable. The library also contains expressions which populate the hashtable:
(library (abc-1)
(export tbl)
(import (rnrs))
(define tbl (make-eq-hashtable))
(hashtable-set! tbl 'a 10)
(hashtable-set! tbl 'b 20)
(hashtable-set! tbl 'c 30))
Here's another version of the library which exports a procedure which can be used to populate the hashtable:
(library (abc-2)
(export tbl init-tbl)
(import (rnrs))
(define tbl (make-eq-hashtable))
(define (init-tbl)
(hashtable-set! tbl 'a 10)
(hashtable-set! tbl 'b 20)
(hashtable-set! tbl 'c 30)))
Is it considered bad form to take the first approach? I.e. to have a library which also executes arbitrary expressions? Are there drawbacks to this approach?
A related issue... In a library, non-definition expressions must occur after definitions. To work around this constraint, I'm using this macro:
(define-syntax no-op-def
(syntax-rules ()
((_ expr ...)
(define no-op
(begin
expr
...)))))
for example:
(define t0 (make-eq-hashtable))
(no-op-def
(hashtable-set! t0 'a 10))
(define t1 (make-eq-hashtable))
(no-op-def
(hashtable-set! t1 'b 20))
Again, are there drawbacks to interspersing expressions and definitions via this workaround?
There are no big problems with either of these. Maybe change no-op
to dummy
to clarify that it's a binding that is never used.
The only possible issue with toplevel side-effect expressions is that in some implementations they might not be executed when you think they should. R6RS allows "implicit phasing", which means that you just import some library and the language gets it into the correct phase depending on where its identifiers are used. So, in such an implementation (eg, Ikarus), if you just import a library but not use its identifiers, the library wouldn't be instantiated -- so a library that is simply used to print some stuff when it's imported will not do so unless it's also exporting some binding, and the importing side is mentioning that binding somewhere.
But that won't be an issue in your case.