NANDAGOPAL
NANDAGOPAL

Reputation: 292

Resources Regarding TCL - OO ( Object Oriented programming in TCL )

I am doing my Internship and the internship requires i learn and practice TCL - OO so i have been hunting for tutorials, examples, books on TCL - OO but i cannot find anything, So i would appreciate it very much if anyone can give me some good advice regarding TCL - OO.

I did some research over the web and came across these materials

Links: http://www.tcl.tk/cgi-bin/tct/tip/257

Book: TCL/TK a developer's guide 3rd edition by clif Flynt - has only 2 chapters on TCL - OO

so besides these two references if anyone can guide me with extra material it would be splendid Thanks in advance

Upvotes: 5

Views: 2828

Answers (3)

Ashok Nadkarni
Ashok Nadkarni

Reputation: 111

A little late, but...

There is a tutorial I posted to http://www.magicsplat.com/articles/oo.html

Upvotes: 7

Donal Fellows
Donal Fellows

Reputation: 137567

Disclosure: I wrote TclOO (with a lot of help from others in the design and testing).


Simple Beginning

TclOO allows very simple use, but can get enormously more complex when you start using a large fraction of its features. Here's a quick sample:

# Make a class
oo::class create Example {
    variable x      ;# Not the same as [variable] in a namespace!
    constructor {} {
        set x 1
    }
    method bar {} {
        return [incr x]
    }
}

Example create foo  ;# Make an instance
puts [foo bar]      ;# Call the instance to get 2
puts [foo bar]      ;# Call the instance to get 3
puts [foo bar]      ;# Call the instance to get 4
foo destroy         ;# Kill the instance

Writing a class is pretty simple, and the above gives you enough to do a lot. There are a few basic features that aren't listed: superclass lets you name the parent class of a class, it defaults to oo::object which is the class of all objects; forward lets you dispatch a method call to another command, a sort-of easy delegation; destructor lets you write something that is called when the object goes away; doing Example new would make an object without naming it, the name of the created object is the result of calling that; the name of the current object is the result of calling self inside a method.

Constructors and methods can take arguments just like the basic Tcl proc command. Destructors can't.

More Complex

Objects can be renamed, just like any other Tcl command, and there is a whole slew of introspection available for them underneath info object and info class. You can attach special per-object behavior to any object with oo::objdefine. Every object has a private namespace which you can use to store state in (that's where the x variable in the above example lives).

Methods are not exported by default if their name doesn't start with a lower-case letter (strictly, it depends on whether it matches the glob pattern “[a-z]*”). You can change this if you prefer.

Classes are themselves objects (instances of oo::class) which is why they are created by calling oo::class create; their constructor passes the script you provide to the command oo::define, which is responsible for defining the behavior of classes. The create and new methods are just that: methods on classes that make instances of those classes (named/unnamed respectively).

You can use multiple inheritance. And mixins. And filters. And add a dispatch handler to deal with attempts to call an unknown method.

You can subclass oo::class itself to let you define new ways of making and managing objects.

You can change the class of any object at runtime (except for oo::object and oo::class; they're specially locked for reasons of sanity).

Yes, I'm the author of TclOO but I'm still exploring what my creation can do. I've tried very hard to ensure that will do virtually anything you ask of it.

Upvotes: 5

Jackson
Jackson

Reputation: 5657

The link you have is for the new OO support that is being included Tcl 8.6 which is still in development, resources on this OO subsystem are likely to be harder to find and while it can be used as is I believe it is aimed more as a foundation for other OO packages.

For Tcl 8.5 and earlier there are a number of 'add on' OO packages, a good starting point for these is here. I've used both incr_Tcl and XOTcl in the past and you should be able to find a reasonable amount of information on them both, their home pages have on-line manuals, tutorials and examples.

Upvotes: 3

Related Questions