A feature which is new in Tcl 8.6a3 is that [namespace ensemble]s can have '''-parameters'''. This page explains this feature and shows some applications. **Explanation** ****What is a parameter?**** In mathematics (and the like, e.g. physics), one sometimes distinguishes between two types of arguments for functions: "normal" arguments, and ''parameters''. The parameters may be separated from the normal arguments by a semicolon (e.g. ''f'' (''x'' ; ''q'')) or bar (e.g. ''F''(''q'',''r'' | ''x'')), but the most common is probably that the parameter appears as a subscript of the main function symbol, such as the ''α'' on Bessel's ''J'' function in [http://en.wikipedia.org/wiki/Bessel_function]. When implemented in [tcllib], this ''α'' is the first of two arguments of the '''[math]::special::Jn''' command, but it is usually only the second argument ''x'' that is considered a normal argument of "the Bessel ''J'' function" — ''α'' is merely a parameter. The difference between parameters and other arguments is ultimately in the eye of the beholder, but some common distinguishing characteristics are: * Parameters don't tend to change as much as normal arguments. * Parameters can be of a different type than ordinary variables (e.g. if one is doing calculus, then integer arguments may be put aside as parameters, since it doesn't make sense to differentiate with respect to them). * The special case of the function that one gets by assigning fixed values to the parameters is still useful to reason about — perhaps even more useful than the general function. In the case of '''[math]::special::Jn''', all of these can be used to label the first argument as a parameter and the second as a normal argument. ****Parameters in Tcl**** In some languages, syntax enforces an explicit distinction between parameters and normal arguments. In for example [http://www.openmath.org/documents/writingCDs.pdf] (PDF document), it says: : It is important to define the mathematically natural concept, rather than the computationally natural one. This may sound obvious, but we will give an illustration: the Bessel functions. Consider the Bessel function (''J''_ν(''z'') for simplicity: the same points are true for other functions). This could be defined, as it is in all numerical libraries, as a function (ℂ×ℂ)→ℂ, [[…]] but it makes more sense to define it as ℂ→(ℂ→ℂ), [[…]] so that we can say that ''J''(''ν'') satisfies Bessel's equation, rather than having to say that λ''x''.''J''(''ν'',''x'') satisfies Bessel's equation. Tcl is '''not''' one of these languages, since our best counterpart of "function" in the above sense is the [command prefix]. A practical definition of "parameter" in Tcl is ''argument included in the command prefix'', since additional arguments provided when calling the command prefix are merely appended to the list of words that are found in it, and passed along to the base command without notice of where they came from; this (or things very much like it) is sometimes called ''currying'' [http://en.wikipedia.org/wiki/Currying] (see also [Custom curry]). As of Tcl 8.5, you can use [list] to curry any command (or command prefix), and the only cost of doing so is that you have to remember to use [{*}] when calling the result of this currying, since it ''is'' a command prefix rather than a command. There was however in Tcl 8.5 one class of commands which wasn't possible to bundle with parameters in a command prefix (or at least not very useful to do so), namely [namespace ensemble]s. The reason for this was that the subcommand name of an ensemble was always the second word in the command; it you regard it as a parameter, then you're always calling the same subcommand, so you might just as well let your command prefix be what this command–subcommand combination is mapped to by the ensemble. This shouldn't be taken as saying a command–subcommand combination is never a useful command prefix (they sometimes are), rather the point is that there was no way to bundle a data parameter for an ensemble into a command prefix without also hardwiring the subcommand used. ****Ensemble parameters**** The solution TIP#314 [http://tip.tcl.tk/314] provides to this problem is simply to add another option '''-parameters''' for [namespace ensemble]s, which controls which word in the command is considered the subcommand name which the ensemble should map to something else. The value of this option is a list of "parameter names", and the ensemble command will step over as many arguments as there are elements in this list before picking one as the subcommand name to dispatch on. The stepped-over arguments are the ensemble parameters, which will appear before the arguments beyond the subcommand name in the target command for the ensemble call. Example (again about Bessel functions, which is a bit of a stretch, but not entirely pointless): namespace eval Bessel { proc J {n x} { # Compute Jn(x) } proc J_zero {n k} { # Compute k'th positive zero of Jn } proc Y {n x} { # Compute Yn(x) } proc Y_zero {n k} { # Compute k'th positive zero of Yn } namespace export * namespace ensemble create -parameters n } After this, one can say set prefix [list Bessel $n] to create a command prefix for "order ''n'' Bessel functions", {*}$prefix J $x to compute ''Jn''(''x''), {*}$prefix J_zero 4 to compute the fourth zero of ''Jn'' (this zero tends to determine the frequency of the fourth overtone of a wave propagating on a circular domain, which is involved in the reason why dinner-gongs and bells have a ringing sound rather a uniform tone), and so on. ''([DKF]: Note that [interp alias] can make these prefixes easier to use. For example, instead of `set pfx {Bessel 7}`, you'd do `interp alias {} Bessel7 {} Bessel 7` or something like that.)'' **Applications** ***[Abstract data types]*** To be continued… ---- !!!!!! %| [Category Command] |% !!!!!!