7.10 Oz Object System

We now illustate the Oz object system by which we can also implement stacks and queues. For missing explanation about Oz object system, we refer to the Oz documentation.

<StackClass>=
class StackClass from BaseObject 
   attr stack:nil
   meth init         stack <- nil  end 
   meth push(E) stack <- E|@stack end 
   meth pop($)  
      Stack = @stack
   in 
      stack <- Stack.2
      Stack.1
   end 
   meth isEmpty($)
      case @stack
      of nil then true 
      else false 
      end 
   end 
   meth toList($) @stack end 
end

<Stack Object>=
unit(push   :proc{$ X} {Stack push(X)} end 
     pop    :fun{$} {Stack pop($)} end 
     init   :proc{$} {Stack init} end 
     isEmpty:fun{$} {Stack isEmpty($)} end 
     toList :fun{$} {Stack toList($)} end)

<NewStack>=
local 
   
<StackClass> 
in 
   fun{NewStack}
      Stack = {New StackClass init}
   in 
      
<Stack Object> 
   end 
end

<Test Object System Stack>=
declare 
<NewStack> 
Stack = {NewStack}
 
in 
 
{Inspect Stack}
{Inspect {Stack.isEmpty}}
{ForAll [1 2 3 4 5] Stack.push}
{Inspect {Stack.toList}}


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