Mini-OOF Example

A short example shows how to use this package. This example, in slightly extended form, is supplied as moof-exm.fs

object class
  method init
  method draw
end-class graphical

This code defines a class graphical with an operation draw. We can perform the operation draw on any graphical object, e.g.:

100 100 t-rex draw

where t-rex is an object or object pointer, created with e.g. graphical new Constant t-rex.

For concrete graphical objects, we define child classes of the class graphical, e.g.:

graphical class
  cell var circle-radius
end-class circle \ "graphical" is the parent class

:noname ( x y -- )
  circle-radius @ draw-circle ; circle defines draw
:noname ( r -- )
  circle-radius ! ; circle defines init

There is no implicit init method, so we have to define one. The creation code of the object now has to call init explicitely.

circle new Constant my-circle
50 my-circle init

It is also possible to add a function to create named objects with automatic call of init, given that all objects have init on the same place:

: new: ( .. o "name" -- )
    new dup Constant init ;
80 circle new: large-circle

We can draw this new circle at (100,100) with:

100 100 my-circle draw