mark roseman

A Busy Developer's Guide to Tcl/Tk 8.6

Current Status of this Document: This document is still in initial draft stage. Last update June 25, 2009.

The Tcl/Tk Core Team has been very busy over the last couple of years (well, before that too!) and Tcl/Tk 8.6 is now in beta, and moving forward quickly towards a release. And there's a lot of really good stuff in it. But it may be a bit hard for your average application developer to sift through what's really important to them.

Like with my Busy Developer's Guide to Tcl/Tk 8.5, this document is intended to quickly introduce "mainstream" developers (admittedly, a completely subjective category) to the important new features in Tcl/Tk 8.6 that they really need to know about. To quote the 8.5 guide:

This isn't a manager's high-level guide, but covers things that affect your code on a daily basis. It's also not something that would interest the type of people who follow daily commits to the Tcl/Tk CVS repositories.

Of course, if you want more comprehensive information, it is definitely out there; start out with the Changes in Tcl/Tk 8.6 page on the Tcler's Wiki, as well as TIP #311, the 8.6 release calendar.

Tcl 8.6:
What Every Developer Needs to Know

Unlike Tcl 8.5, which introduced a lot of new idioms that did away with big ugly warts scattered throughout almost everyone's code, there's only one thing in Tcl 8.6 that I think application developers really should be aware of.

Core Support for Object Orientation

Since the dawn of time (well, at least the very first Tcl/Tk workshop in 1993), there's been a push to have an object system included in the core of Tcl, and over the years there have been dozens, if not hundreds, that have been written and put forward as evidence that objects must be in the core. Many hundreds more personal implementations have also been created; writing an object system for Tcl has been considered by many a rite of passage. To speculate on the untold thousands of hours spent on political arguments about this topic brings a tear to my eye.

Suffice it to say, there's never been a shortage of options for doing object oriented programming in Tcl.

About the Implementation. As of Tcl/Tk 8.6, there's now an "official" object system, living in the "oo::" namespace, that you can know is available anywhere you find Tcl 8.6 or newer. It's got the range of expressive power, flexibility, dynamic nature, extensibility and raw performance you'd expect. This doesn't preclude using other systems, and in fact some of them have rewritten their internals to take advantage of the infrastructure provided by the core system.

A full description of the OO system can be found in TIP #257 as well as the oo::class, oo::object, and oo::define reference manual pages.

Implications for Developers. This doesn't mean you should run out and convert all your existing procedural code to use object oriented code, nor convert code written in another object system to use the core one. What it does mean is that you now have a "no hassle" object system available to you, which makes deciding about whether or not to create some new code procedurally or with objects simply an issue of which is the better fit.

If you're using a different system already, or a homegrown one, there may be some very impressive performance gains to be had to migrate your code over, whether directly on top of the core "oo::", or another object system that leverages that system. It also ensures that the object system will be maintained and synchronized with new releases of Tcl.

Tcl 8.6: Other Things You Should Know

While there are a lot of new things in Tcl 8.6, those below deserve a particular mention for mainstream application developers.

Tcl Database Connectivity. There is now a standard database abstraction layer available as part of the Tcl 8.6 core, which provides a similar front end interface to different underlying database engines. As with OO, there have long been several competing options in this area, as well as of course the database-specific API's. For details see TIP #308, TIP #350, as well as the TDBC manual pages (in the 8.6 distribution, link forthcoming). Initial database drivers are available for MySQL, ODBC, and SQLite3.

Try/Catch/Finally. There is a new syntax for handling exceptions, based on the "try" syntax used by many other languages. This complements, rather than replaces, the existing "catch" mechanism, and in fact adds no new capabilities, but does provide a cleaner way to manage certain types of error handling.

Doing Nothing is Okay. Many Tcl commands which expected a variable number of arguments (such as "file delete") would generate an error if called with no arguments. As of 8.6, many of them now allow this (deleting zero files? no problem!), which should save more than a few code checks for empty lists. See TIP #323 for the affected commands.

Compression. There is now an interface to zlib included as part of the Tcl core; see TIP #234.

Temporary files. There is now a standard "file tempfile" (see TIP #210) for creating temporary files.

NRE and Coroutines. Ok, this is not something that most developers need to worry about, but is really geeky cool. Tcl's internal execution engine has been replaced by a "Non-Recursive-Engine" (NRE), which means that every new layer of code evaluation does not create a new layer on the C stack, resulting in massively reduced stack usage. This is conceptually similar to the "stackless Python" implementation in that language. As a result, some new ways of structuring applications become more readily available; Lua-inspired coroutines (see TIP #328) are now available in the core as a result of NRE.

Tk 8.6:
What Every Developer Needs to Know

On the Tk side, there is not as dramatic a change as in 8.5, which introduced the themed Tk widgets. Under the hood, there have of course been many improvements and bug fixes to those themed widgets. But only a few things are particularly worth noting.

Mac Tk Switching to Cocoa

Very significant for Mac developers, or those targeting Mac OS X, the underlying implementation of Tk is switching from Carbon (a transitional API to help developers with the switch from classic MacOS to OS X, and now deprecated by Apple) to Cocoa (the native user interface API on Mac OS X).

While many of the Carbon and Cocoa API's just called each other and therefore did the same thing anyway, the switch to Cocoa will provide a few immediate improvements in the native look and feel of Tk on OS X, and the potential for increased improvements in the future. Perhaps more importantly, this provides a solid foundation for Tk on the Mac to improve and maintain compatibility with future Mac OS X releases.

PNG Image Support

The Tk core now adds native support for PNG images, without needing any extensions (see TIP #244).

Font Chooser

The other thing of note, because it is used so often in applications, is that Tk now includes a standard font selection dialog (see TIP #324). If you're using something else, replace it with this.

Tk 8.6: Other Things You Should Know

Again, a few things that may well improve your programs, some without any changes needed on your part.

Angled Text. The canvas widget now supports the creation of angled text (see TIP #119).

Blocking all Events. The new "tk busy" command (adopted from BLT's blt_busy) now allows an application to block out all user interaction events, for example while performing a long operation (see TIP #321).

MouseWheel Improvements. The default mouse wheel bindings have been improved so that they work "properly", scrolling the widget that is underneath the mouse, regardless of whether it actually has the focus.