Version 7 of Ensemble

Updated 2002-12-16 17:49:08

Purpose: discuss the concept of command ensemble

This concept has also been called "major/minor" commands by some people (like myself) who didn't know the fancier name of ensemble.

[Question - is this what other languages may call mixins?]

Tcl has several kinds of commands.

The first, a simple command like set, provides a very simple interface. One passes either one or two arguments. Depending on the number of arguments, set either outputs the value of the variable, or it sets the variable to a value.

A slightly more complex variation of this is a command that takes arguments. puts for instance has an optional -nonewline flag as well as an optional output channel and an output string.

Next in complexity comes the ensemble. An example of this command would be string. What makes it more complex is that string is an umbrella name for a variety of related functionality. String is the major command name, the minor subcommand names are things like bytelength,compare, etc.

See TIP http://purl.org/tcl/tip/112.html for a TIP to include support for developing this type of command in Tcl.


Since every object command is an ensemble, having support for ensembles as part of the language (presumably coded in C) would make pure Tcl object and megawidget frameworks much more efficient. -- WHD


LV So how do you document extendable ensembles? For instance, let's say we create a new command called xtsil. It's designed to be extendable from either script or some API. The original command's documentation would be xtsil in section n. Now assume that J. Random Programmer (JRP) comes along with an implementation of xtsil stdev. Certainly s/he could contribute the code to the original author for integration. But that may not always work - two people might have alternative implmentations (or even different functional implmentations for the same subcommand name).

In some languages, each subcommand gets its own reference doc file, which takes care of this problem (but does result in a LOT of reference docs).

WHD: I'd argue that extending someone else's ensemble in the manner you describe is a no-no, as it muddies the water for maintenance developers. That is to say, even if the capability existed, I wouldn't use it. I might define my own ensemble which delegates most of its subcommands to some other ensemble, and adds a few of its own; and I'd document by saying "myxtsil is just like xtsil, but adds these subcommands...."

LV: Then in your opinion, there only reason to have such a command is for implementing a new command?

RS notes that BWidget constructs its method names in the pattern Class::method, therefore enhancing BWidget is very easy: by just writing a proc

 proc Foo::dance {self what} {...} 
 ;# ...you can instruct an object of class Foo to:
 myFooInstance dance polka

See also Wrapping Commands


Category Tutorial