| << Prev | - Up - | Next >> |
So far, we have either used modules directly available in the interactive environment or explicitly obtained new ones by invoking Module.link or Module.apply. When writing functors, a better alternative is available: using import declarations. Let's write a module that exports a Greet procedure which prints hello world!:
declare
functor Foo
import
System at 'x-oz://system/System.ozf'
export
greet : Greet
define
proc {Greet}
{System.showInfo "hello world!"}
end
end Let's have a look at this functor value with {Inspect Foo}:

As we can see, the functor indicates that the module it describes exports a nullary procedure on feature greet and that it imports one module from URI x-oz://system/System.ozf. Let's try it out immediately thus:
declare [M]={Module.apply [Foo]}
{M.greet} The import section begins with the keyword import and contains one or more import specifications. Here there is only one:
System at 'x-oz://system/System.ozf' it states that variable System should be bound to the module which can be constructed from the functor at the URI identified by atom 'x-oz://system/System.ozf'. Instead of an abstract URI, you could also have a file name or a URL. For libraries which are part of the Mozart system, a shorter form of imports is also supported:
import Systemis exactly equivalent to:
import System at 'x-oz://system/System.ozf'That is: the name of the variable is automatically used to construct the appropriate URI. For modules not part of the Mozart library you must use the long form. Also for exports, there is a similar kind of short form:
export Greetis exactly equivalent to:
export greet : GreetThe name of the variable is used to infer the name of the export feature by downcasing its first letter. Thus our functor can be more simply written:
declare
functor Foo
import System
export Greet
define
proc {Greet}
{System.showInfo "hello world!"}
end
end| << Prev | - Up - | Next >> |