TclOO Tutorial

[extracted from this c.l.t posting ]

For very basic usage, here's this. It assumes you know a bit about OO systems in general.

Basic TclOO Usage

Declare a class like this:

  oo::class create MyExampleClass { 
      constructor {args} {# like [proc] 
          # Do something with the args... 
      } 
      # Public methods begin with lower-case letters 
      method foo {bar boo} {# like [proc] 
          puts "bar=$bar, boo=$boo" 
      } 
      destructor {# No arguments 
          # Clean up here if necessary 
      } 
  }

Use that class/object like this:

  MyExampleClass create exampleObj someArgumentToConstructor... 
  exampleObj foo 1 2;    # prints 'bar=1, boo=2' 
  exampleObj destroy;    # wipes the object out 
  set obj [MyExampleClass new];  # 'Unnamed' object 
  $obj foo 3 4;          # prints 'bar=3, boo=4' 
  $obj destroy 

State

Every object (i.e., every instance) has its own arbitrarily-named namespace that is not shared with anything else. It is the current namespace when a method (or constructor or destructor) is executing. Store state in there, perhaps like this:

  oo::class create Example2 { 
      constructor x { 
          variable X $x 
      } 
      method addToX y { 
          variable X 
          set X [expr {$X + $y}] 
          return 
      } 
      destructor { 
          variable X 
          puts "X is $X" 
      } 
  } 
  Example2 create eg 3 
  eg addToX 5 
  eg addToX 2 
  eg destroy;             # Prints 'X is 10' 

Introspection

Introspection is done by info class, info object and (within methods only) self. For example, the name of an object's private namespace is given by info object namespace $obj. By itself, self returns the name of the currently-executing object. See the documentation for details.

Private Methods

Private methods are methods whose names don't begin with a lower-case letter. They can be invoked on an object by using my instead of the object name, like this:

  ... 
  method ThePrivateMethod ... ... 
  method aPublicMethod ... { 
      puts "before the private call" 
      my ThePrivateMethod ... 
      puts "after the private call" 
  } 
  ... 

The my command is always located in the object's private namespace. This makes it ideal (with a very little trickery) for handling callbacks like traces and events.

Going Deeper

This is where we get into the next level of understanding about TclOO.

Definitions

Classes delegate their definitions to the command oo::define, which you can also use directly to add or update definitions to a class. You can also use oo::objdefine to apply definitions directly to an object (almost as if it is an instance of its own private subclass; that's not what happens but it looks very similar). You can even update the definitions on a class after instances of it have been created and have those instances pick up the changes. After all, Tcl's a dynamic language!

  oo::class create Example2a
  # One syntax style: single argument is definition script
  oo::define Example2a { 
      constructor x { 
          variable X $x 
      } 
      destructor { 
          variable X 
          puts "X is $X" 
      } 
  } 
  Example2a create foo 3
  # Other syntax style: multiple arguments give a single definition
  oo::define Example2a method addToX y { 
      variable X 
      set X [expr {$X + $y}] 
      return 
  } 

Expert stuff

Classes are objects themselves (instances of oo::class) which is why the syntax for making a class is so much like the syntax for making a named object. Because it's exactly the same. This is a deep rabbit-hole, but I'll stop writing this up here. :-)


jblz Other helpful resources include: The original TclOO TIP , and its companion . There is also quite a bit of general purpose TclOO code in the libraries of wub , if one is looking for examples of TclOO in action.


LV - 2011-01-19 08:00:29

I just noticed Moving objects - first steps with TclOO and thought it might serve as an example of using tcloo.


arjen - 2011-01-20 03:05:51

Here is a less trivial example of using TclOO where I am a bit stuck: Multiple inheritance with TclOO - how to resolve ambiguities


APN My attempt at a tutorial is in the form of an article . Please redirect any comments or corrections to the BOOK Tcl Programming for Windows? page.