<< Prev | - Up - |
We now change from the Oz library style to that of object oriented programming. Cell objects are modelled by records of procedures that give access to a hidden library cell. The type of a cell object is:
unit(get : -> value
put : value ->
exchange: value -> value
...)
Cell objects can be created by calling a creation function NewCellObject
(the cell's class) while passing the initial value.
functor
export
new:NewCellObject
define
fun{NewCellObject Init}
C = {Cell.new Init}
fun {Get} {Access C} end
proc{Put Value} {Assign C Value} end
in
unit(get:Get
put:Put
exchange:fun{$ New}
Old
in
{Exchange C Old New}
Old
end
access:Get
assign:Put)
end
end
This functor defines a module which exports the function NewCellObject
at feature new
.
The procedures of cell objects are defined by reduction to the corresponding library functions. Essentially, they do nothing else than applying the library functions to the concrete hidden cell.
But note that we also did some more changes and less systematic chages for our convenience. We turn the binary library procedure Cell.exchange
into a function Exchange
that inputs a value to be put into the cell and outputs the old of the cell.
Furthermore, we have also adapted the name of the functions for convenience. We use put
and get
as usual in the library rather than Access
and Assign
. Finally, we have turned Exchange
into a function that inputs the new value and returns the old one. Note that the input argument New
is often left variable in applications. We can now use bags by linking a compiled version of the module Bag.ozf
that is made available at the following URL
:
AuthorURL = 'http://www.ps.uni-sb.de/~niehren'
CourseDir = '/Web/Vorlesungen/Oz-NL-SS01'
CourseURL = AuthorURL#CourseDir
PickleVersion= 'Version.3.2'
DataDir = '/vorlesung/Functors/'#PickleVersion
URL = CourseURL#DataDir
This allows you to test the above implementation of cell objects.
declare
<URL>
declare CellURL = URL#'/Cell.ozf'
declare [CellMod] = {Module.link [CellURL]}
declare C={CellMod.new 2002}
{Inspect {C.get}}
{C.put 4711}
{Inspect {C.put}}
declare NewVal OldVal = {C.exchange NewVal}
{Inspect OldVal}
NewVal=OldVal-42
{Inspect {C.get}}
<< Prev | - Up - |