24.2 Functors

A functor is a piece of compiled Oz-code. A functor can be stored and accessed from a web address.

Oz provides extra values for describing web-addresses (URL's), so called virtual strings. Virtual strings are tuples composed from hashes and atoms. Virtual stings allow the programmer to compute with URL's.

<URL2>=
PSLab = 'http://www.ps.uni-sb.de' 
Dir='~niehren/oz-course.html/Functors' 
PickleVersion = 'Version.3.2' 
fun{Conc URLs}
   case URLs  
   of [U] then U
   [] U|Us then U#'/'#{Conc Us}  
   end      
end 
URL= {Conc [PSLab Dir PickleVersion]}

A functor is a value in Mozart. This means that a variable can have a functor as its value, i.e. the value of a variable can be a piece of compiled Oz-code.

<Functors as Values>=
<URL2> 
FileName='NewChart.ozf' 
File = {Conc [URL FileName]}   
NewChart_Functor={Pickle.load File}

The procedure Pickle.load returns a functor when given an URL. A more interesting thing you can do with a functor is to turn it into a module, which is loaded on need.

<Functor To Module>=
<URL2> 
[Mod1 Mod2] = {Module.link [{Conc [URL 'NewChart.ozf']}
                            {Conc [URL 'ChartToWindow.ozf']}]}

The procedure Module.link inputs a list of URL's and output a list of modules (which are loaded on need).

<Browse Modules>=
declare  
<Functor To Module> 
{Browse [Mod1 Mod2]}        % the modules Mod1, Mod2 are not yet loaded
                            % even though their future value is defined
/*
{ForAll [Mod1 Mod2] Wait}   % {Wait X} waits until a future value  
                            % for X gets defined (as for instance
                            % by {Module.link [X]}) and then loads  
                            % this value.
*/
 


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)