Next: , Previous: , Up: libutils   [Index]


7.9 Dynamically loading Scheme libraries

It is possible to dynamically load a Scheme library after the program has been compiled. Here is an example of: creating a library, importing it, then dynamically loading its syntactic bindings:

(import (vicare)
  (prefix (vicare libraries) libs.))

(library (demo-01)
  (export fun var)
  (import (rnrs))
  (define (fun a b)
    (+ a b))
  (define var 123))

(import (demo-01))

(let* ((lib (libs.library-dynamic-load-and-intern '(demo-01)))
       (fun (libs.library-dynamic-retrieve lib 'fun))
       (var (libs.library-dynamic-retrieve lib 'var)))
  (values (fun 1 2) var))
⇒ 3 123

another example which loads the installed (vicare pregexp):

(import (vicare)
  (prefix (vicare libraries) libs.))

(define-values (pregexp-match)
  (let ((lib (libs.library-dynamic-load-and-intern
                 '(vicare pregexp))))
    (values (libs.library-dynamic-retrieve lib 'pregexp-match))))

(pregexp-match "[a-z]+" "ciao hello ciao")
⇒ ("ciao")
Function: library-dynamic-load-and-intern libref

Search for a library whose name matches the library reference libref, intern it and invoke it. When successful return a <library> object. If an error occurs: raise an exception.

This function makes use of find-library-by-name to search and intern the requested library.

Function: library-dynamic-retrieve lib name

Search in the GLOBAL-ENV of the <library> object lib an exported syntactic binding whose public name is the symbol name. When successful: return the value bound to the syntactic binding. If an error occurs: raise an exception.