Next: , Previous: , Up: iklib modules examples   [Index]


6.36.2.2 Named modules examples

The following example defines a module named blue and invokes functions from it in the top level region:

(import (vicare))

(module BLUE
    (blue-one blue-two blue-three)
  (define (blue-one)          'blue-one)
  (define (blue-two)          'blue-two)
  (define (blue-three)        (cons 'blue-three (hidden)))
  (define (hidden)            'blue-hidden))

(import BLUE)
(fprintf (current-error-port)
         "calling blue: ~s ~s ~s\n"
         (blue-one) (blue-two) (blue-three))

notice that bindings from a named module are accessible only if imported in the enclosing region.

The following example defines two modules named green and red, then it imports their bindings in different regions:

(import (vicare))

(internal-body

  (module GREEN (one two three)
    (define (one)       'green-one)
    (define (two)       'green-two)
    (define (three)     (cons 'green-three (hidden)))
    (define (hidden)    'green-hidden))

  (module RED (one two three)
    (define (one)       'red-one)
    (define (two)       'red-two)
    (define (three)     (cons 'red-three (hidden)))
    (define (hidden)    'red-hidden))

  (import GREEN)

  (internal-body
    (import RED)
    (fprintf (current-error-port)
      "calling red: ~s ~s ~s\n" (one) (two) (three)))

  (fprintf (current-error-port)
    "calling green: ~s ~s ~s\n" (one) (two) (three)))

The following example shows that modules in the same enclosing region can import their bindings:

(import (vicare))

(internal-body

  (module green (one two)
    (define (one)       'one)
    (define (two)       'two))

  (module red (f g)
    (import green)
    (define (f)         (cons 'f (one)))
    (define (g)         (cons 'g (two))))

  (import red)
  (fprintf (current-error-port)
    "calling red: ~s ~s\n" (f) (g)))

while the following example shows that bindings from an anonymous module are automatically available in modules defined in the same enclosing region:

(import (vicare))

(internal-body

  (module (one two)
    (define (one)       'one)
    (define (two)       'two))

  (module red (f g)
    (define (f)         (cons 'f (one)))
    (define (g)         (cons 'g (two))))

  (import red)
  (fprintf (current-error-port)
    "calling red: ~s ~s\n" (f) (g)))

and the following example shows that the order of module definitions does not matter in determining visibility of bindings from anonymous modules:

(import (vicare))

(internal-body

  (module red (f g)
    (define (f)         (cons 'f (one)))
    (define (g)         (cons 'g (two))))

  (module (one two)
    (define (one)       'one)
    (define (two)       'two))

  (import red)
  (fprintf (current-error-port)
    "calling red: ~s ~s\n" (f) (g)))

the following is an error because it tries to import bindings from a named module before its definition:

(import (vicare))

(internal-body

  (module red (f g)
    (import green) ;; error here!!!
    (define (f)         (cons 'f (one)))
    (define (g)         (cons 'g (two))))

  (module green (one two)
    (define (one)       'one)
    (define (two)       'two))

  (import red)
  (fprintf (current-error-port)
    "calling red: ~s ~s\n" (f) (g)))

Next: , Previous: , Up: iklib modules examples   [Index]