scheme - Exporting a populated hashtable from a library -


here's library exports hashtable. library contains expressions populate 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 version of library exports procedure can used populate 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 considered bad form take first approach? i.e. have library executes arbitrary expressions? there drawbacks approach?

a related issue... in library, non-definition expressions must occur after definitions. work around constraint, i'm using 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, there drawbacks interspersing expressions , definitions via workaround?

there no big problems either of these. maybe change no-op dummy clarify it's binding never used.

the possible issue toplevel side-effect expressions in implementations might not executed when think should. r6rs allows "implicit phasing", means import library , language gets correct phase depending on identifiers used. so, in such implementation (eg, ikarus), if import library not use identifiers, library wouldn't instantiated -- library used print stuff when it's imported not unless it's exporting binding, , importing side mentioning binding somewhere.

but won't issue in case.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -