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, the 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 polkaGN There is nothing special about BWidget in this respect, XOTcl does it as well since ages and provides an unknown method for each object/namespace (see XOTcl Objects as Tcl Commands with subcommands).
LV Then how should one document the dance method for this object? Should each object have a separate reference page for each method (whether HTML, man, or some other format) so that all one needs to do is add a new page?
See Wrapping Commands, stacking for some examples of creating ensembles.
RFox The issue of merging documentation for random programmer implemented subcommands in an ensemble with the original package documentation is only important if these subcommands get contributed back to the original developers. Now if, instead of using those drudgy man pages, we started using Wiki based documentation (as some starkits do), all this is simple, just edit the wiki page that documents the ensemble and add in your subcommnds. Now you've got the local site documents updated.In any open source system that accepts external contributions, there's always some release manager/management and part of the job of the release manger is merging documentation from the various sources.
LV It seems like most developers wouldn't bother to track every ensemble change that every package require invoked might perform - or the internal ones that each one used do - and do all that doc integration.
EKB In case this isn't already well known, it's easy to create an ensemble with snit:
snit::type mytype { option -greeting "Hi!" option -parting "Bye!" method greet {} { return $options(-greeting) } method part {} { return $options(-parting) } }Then use it, e.g.,
mytype myobj -greeting "Hello!" puts [myobj greet] puts [myobj part]It can then be extended by delegation. My comment on that discussion is that it seems best to give an extended ensemble a new name, as a way to relieve the documentation headaches. Steps for one proc per subcommand
HaO An application of ensembles is to have a proc per subcommand. Here are the necessary steps:
namespace eval ::commandname { namespace export sub1 sub2 namespace ensemble create } proc ::commandname::sub1 {args} { puts sub1=$args } proc ::commandname::sub2 {args} { puts sub2=$args }And here is how to call them:
% commandname sub1 a b c sub1=a b cAs a more practical example, use a command counter, which may set, get or increment a number:
namespace eval ::counter { variable counter 0 namespace export get set incr namespace ensemble create } proc ::counter::get {} { variable counter return $counter } proc ::counter::set {value} { variable counter # the "::" in front of set is necessary to not call ::counter::set return [::set counter $value] } proc ::counter::incr {increment} { variable counter return [::incr counter $increment] }And here how to use it:
% counter set 5 5 % counter incr 5 10 % counter get 10Don't get irritated that ensembles are a mapping of a b -> a::b. To test this:
% namespace eval counter {namespace export showlevel} % proc ::counter::showlevel {} {return [info level 1]} % counter showlevel ::counter::showlevelRS 2015-05-13 A short interactive session to discover which Tcl commands are implemented as ensembles:
% set ens {}; foreach c [info commands] {if ![catch {namespace ensemble configure ::$c -map}] {lappend ens $c}} % set ens chan clock info dict stringNote you can use namespace ensemble exists instead of catching the error.
% lmap c [info commands] { if {![namespace ensemble exists $c]} continue; set c} binary tk array chan info file namespace dict string