Fun with Tcl

Sarnold March 2005 Purpose of this page : tell how different programming styles are possible with Tcl, and how it saves headaches, rather than just tell someone : Tcling is fun, because you won't be believed until you show what makes it possible.

And IMHO, programming is too serious to be fun, even if the programmer often searches the Holy Grail of the languages. The discussion is open.

Functional Programming

Tcl makes it possible, with a common style, and even with some Radical Language Modifications. It is well known to reduce side-effects. Maybe would it reduce the size of your scripts.

Every Tcler has had to deal with FP-like structured programming, even without the help of Tail call optimization, RPN or whatsoever.

Recursivity is often a good way to design algorithms, whereas non-recursive programs may be faster. Sometimes, designing a functionnality is just wasteful without recursivity.

And more : FP have lambdas and apply to reduce the needs for variable names, which eases the pain to name every little variable.

Object Orientation

The choice is here : many OO extensions are available (it means no OO feature is present in the core). It helps you to structure your code , and doesn't need wasteful learning. There are good tutorials, and it is, in most cases, not boring to write some classes.

When you have lots of business logic, OOP will be a strong choice.

It may be used to hide the implementation details of a mega-widget, and to give a Tk-like control interface of new widgets. It could be used to enable polymorphism, but you would have to care seriously of the OO-design.

But most of the time, object-orientation are used to give a syntaxic sugar. Structured programming is quickly done, but may turn queer if you structure data the wrong way. It is often easier to structure your code when you are using OOP-extensions, and IMHO code you wrote is easier to read 6 months later.

One big thing about OOP in Tcl is that there are compiled extensions, and pure-Tcl extensions, the latter being far slower than the former.

Two major compiled OO systems :

  • incr Tcl which is modelled after C++ (like the name)
  • XOTcl which is more Tclish, and allows some kind of metaprogramming

One major pure-Tcl OO system : snit, which is based on delegation rather than inheritance.

Advantages and disadvantages of OOP

Object vs. Megawidget

Regular Tcl Style

After all, you can get the job done with the basic style, with the help of Introspection. For small scripts, you won't need a complete knowledge of the commands (memoizing manual pages).

You can write GUI programs quickly, with patterns that split the view from the data model, even with plain namespaces.

You can test your program interactively, with tclsh or other tools like tkcon. Separating blocks of code and testing it is easy ; you do not need to remember all commands' manuals, because you can always test a more or less sophisticated bit of code in a console with little effort.

An aphorism :

 "Write code so simple so that bugs just can't happen."

(Well, you wouldn't believe me, but this simplicity exists) As you won't need to know the complete Java API, you won't need to know all the core Tcl documentation to write maintainable scripts.

Many patterns are done by using small procedures which encapsulate data, and are used in a foreach control structure. Tcl is often said to have a procedural nature, and introspection is probably Tcl's strength.

TODO : explain why scripts written in a regular style are easy to understand, and finally to maintain.

Dynamic language

There are many ways to do things dynamically, creating procs at runtime, treating variables as strings, and so on. You may realize that a functionality is done in 10 lines, where in imperative programming it would take ten times more.

More than one way to do that !

This is common in many languages, but much more in Tcl.

Fun with Chinese Characters

JimG I have tried some languages before I started to learn TCL recently and I find it's fun to program Tcl in Chinese.

EKB 24 April 2005

Well, I can never quite shake the thought that maybe I like Tcl/Tk because I'm used to it. (I'm also just a so-so Tcl/Tk programmer - RS's posts in particular blow my mind.) But setting that aside, here are a couple of specifics:

Rapid GUI Development

Wearing my software developer's hat, I do careful and deliberate GUI development. I use Tcl/Tk for that, but could use other tools. But wearing my researcher and teacher's hats, I need to whip out a program for others to use in a very short time. Nowadays everyone expects a GUI, and Tcl/Tk is great for quickly creating a GUI. People say, "Ooh, thanks!" to me and everybody's happy.

Make the language your own

Tcl is like Forth in that you program by extending the language. You can really make the language your own. This makes it easy to first create a core program, then expose your Tcl commands to the user so he or she can modify your program. It's a nice way to make a powerful, extensible program.

Create new semantics

or Tcl: The Chameleon Language

[I have heard that Perl could, too. Is it true?]