Tcl HomeTcl Home hosted by

Google SiteSearch

Object-Oriented Programming in Tcl

You'll sometimes hear that Tcl doesn't have support for object oriented programming. While Tcl doesn't force you to write object-oriented code (probably good if you're throwing together a ten line script), you certainly can write object-oriented code — many ways.

Tcl has multiple object systems. Tcl is built to be easily extended, and over the years, many different object-oriented extensions have been built and used. The first Tcl workshop, back in 1993, had several papers about such systems, and more have appeared with each successive year. This is perhaps no surprise, given the many ways to do object-oriented programming in the computing world.

So for Tcl developers, they've always had object-oriented programming systems available to them, full-featured, powerful, well documented, and widely used. They just weren't built in to the core language, so people had to look around at some of the best options and choose one that fit their needs. Or, in what seems to be a Tcl rite of passage, they ended up writing their own, just because they could, and because Tcl makes it so easy to do so.

Choice has its downsides. The choice in different systems is a good thing, but it has its downsides. For newcomers, they don't immediately see that there are these systems readily available to them. And even if they know about them, when you're just learning a language, are you really in the best position to choose the best object-oriented tool? Sometimes it's nice to have people tell you "just use this". As well, not having a "standard" or "default" choice sometimes means effort will be duplicated rather than focused, so you lose out on some positive network effects.

Tcl isn't alone in this of course. Saying Tcl has no object system is like saying Python has no web framework. The issue is really there are too many of them. Contrast that with a language like Ruby, which while it has multiple ways to do web programming, if you ask anyone, they'll just say "Rails" as the default. On the other hand, if you ask Tcl people about GUI programming, they'll all say "Tk" (even though there are other ways available), while if you try asking that to a group of Perl, Python, Ruby, etc. users you'd better be prepared for a long drawn out discussion of the many competing alternatives.

So what to choose? So given that there are so many choices, what's a Tcl programmer to do? [incr Tcl] is a mature object system for Tcl (been around since 1993) that's modelled after a familiar C++-style object model. Snit has a delegation-based model that's great particularly for Tk widgets, and is included in Tcllib. XOTcl has many advanced features found in today's cutting edge object systems. And of course, there are many others.

Coming soon. Tcl has a blessed foundational object system along with the Tcl core in 8.6 as specified in TIP #279, so that everywhere you have Tcl, you have a flexible, high performance and standard object system — guaranteed. Based on XOTcl and snit, the system is rich enough that emulation layers for other existing object systems can be easily layered on top of it.

Having choice in object systems is a great thing, and shows the flexibility of Tcl. This new proposal will simply mean you aren't forced to make the choice if you don't want to.