Version 16 of SDynObject

Updated 2004-03-02 07:12:36

SDynObject is short for Simple Dynamic Object system.

The latest SDynObject is available (with demos) from (some widgets incomplete due to lack of interest): http://www.xmission.com/~georgeps/SDynObject/

License: Tcl style


SDynObject is a dynamic object-oriented (OO) system. It features multiple-inheritance, fast execution,

 and simplicity.

Most OO systems have a class command. In SDynObject we don't need such a thing, because proc provides all that we need. For example:

 proc Bat obj {
  sd.new.method hit.ball for $obj takes ball {
   #at this point we have ball passed to us.
  } 
  return $obj
 }

How would we use the class given above you may ask? Well, it's really quite easy. First we need to cr eate an object and then we pass the object to the Bat proc so that it inherits hit.ball. For example:

 set b [Bat [sd.new.object]]
 $b->hit.ball someball

The $b->hit.ball command is just a normal Tcl procedure.

If you wanted to create a method that takes any arguments you can use the standard args string for the takes argument.

How do we deal with instance variables in SDynObject? This is best illustrated with an example.

 proc Bat obj {
  $obj v foo
  ...
 }

In the example above we have just set the variable v and it will be held within our object.

How do we reference the given variable:

 proc Bat obj {
  $obj v foo
  puts [$obj v]
  return $obj
 }

In the example above puts $obj v will print foo.

How do we access the object that refers to the method we are in?

 proc Bat obj {
  sd.new.method hit.ball for $obj takes ball {
   $self v $ball
  }
  return $obj
 }

In the example above $self is like the $obj. It's the same token value. $self connects all methods to

 a single object.  You can use $self to call methods within an object too, as in $self->some.method.

Now how do we create a method within a method?

 proc Bat obj {
  sd.new.method create.anew for $obj takes name {
   sd.new.method $name for $self takes args {
    puts "$self $args"
   }
  }
  return $obj
 }

How is multiple-inheritance done in SDynObject? We use several procedures and an object is passed to t hem so that it inherits methods and variables. For example:

 proc Object obj {
  $obj x 1
  $obj y 1
  $obj z 1
  $obj name Object

  sd.new.method move for $obj takes {x y z} {
   $self x $x
   $self y $y
   $self z $z
  }
  return $obj
 }

 proc Ball obj {
  $obj name Ball ;#inherit methods and variables from Object

  sd.new.method roll for $obj takes direction {
   if {"n" == $direction} {
    #roll ball north
   } elseif {"e" == $direction} {
    #roll ball east
   }
   ...
  }
  return $obj
 }

 set b [Ball [Object [sd.new.object]]]

 $b->move 5 4 3
 $b->roll n

Tcl and Tk use a -textvariable option that is very handy, but how do we do such a thing with SDynObject

 instance variables?  

 proc Class obj {
  $obj v foo
  return $obj
 }

 set c [Class [sd.new.object]]

 pack [entry .e -textvariable [sd.dereference $c v]]
 #.e should now display foo

How do we destroy an object's variables and methods?

 sd.destroy.object $obj

Note: I may eventually make it be done via $obj->destroy.

For further information I suggest that you study SDynObject.tcl It's less than 50 lines of code and IM O quite easy to understand.


Category Object Orientation | Category Package |