next up previous

10.4.2 Importing Constructs

The import specification in a defmodule definition is used to indicate which constructs the module being defined will use from other modules. Only deftemplates, defclasses, defglobals, deffunctions, and defgenerics may be imported.

There are three different types of import specifications. First, a module may import all valid constructs that are visible to a specified module. This accomplished by following the import keyword with a module name followed by the ?ALL keyword. Second, a module may import all valid constructs of a particular type that are visible to a specified module. This accomplished by following the import keyword with a module name followed by the name of the construct type followed by the ?ALL keyword. Third, a module may import specific constructs of a particular type that are visible to it. This accomplished by following the import keyword with a module name followed by the name of the construct type followed by the name of one or more visible constructs of the specified type. In the following code, defmodule A imports all of module Dís constructs; defmodule B imports all of module Dís deftemplates; and defmodule C imports the foo, bar, and yak defglobals from module D.

(defmodule A (import D ?ALL))
(defmodule B (import D deftemplate ?ALL))
(defmodule C (import D defglobal foo bar yak))

The ?NONE keyword may be used in place of the ?ALL keyword to indicate either that no constructs are imported from a module or that no constructs of a particular type are imported from a module.

Defmethods and defmessagehandlers cannot be explicitly imported. Importing a defgeneric automatically imports all associated defmethods. Importing a defclass automatically imports all associated defmessagehandlers. Deffacts, definstances, and defrules cannot be imported.

A module must be defined before it is used in an import specification. In addition, if specific constructs are listed in the import specification, they must already be defined in the module exporting them. It is not necessary to import a construct from the module in which it is defined in order to use it. A construct can be indirectly imported from a module that directly imports and then exports the module to be used.


next up previous