Re: Tcl Switch case
- From: "tom.rmadilo" <tom.rmadilo@xxxxxxxxx>
- Date: Thu, 21 Feb 2008 10:54:55 -0800 (PST)
On Feb 21, 9:10 am, Bryan Oakley <oak...@xxxxxxxxxxxxxxxxxxxx> wrote:
tom.rmadilo wrote:
switch ?options? word pattern body pattern body ....
That's not a new form. It's been around since version 7.6.
http://www.tcl.tk/man/tcl7.6/TclCmd/switch.n.html
Eeek! You are right. Going back to the 'mess' which is switch. This is
a unique command in several ways. First is that it is a control
structure. I'll have to look, but I don't know of any other Tcl flow
control commands which take options. But something else was added to
this mess in 8.5: options which take values! the -matchvar and -
indexvar options greatly complicate the situation. In many Tcl
commands I have argued that switches help unite common operations into
a single whole, but in the case of switch, the options seem to have
the opposite effect. The options have a drastic effect on the intended
operation.
And unless you are the unfortunate maintainer of the code, or charged
with creating the byte code for this monster command, it doesn't look
like too much of an issue. To visualize the difficulty to myself, I
just wrote up a replacement ensemble command for [switch]. To handle
the previous 8.4 options, there are six distinct subcommands, although
I broke these into two categories (nocase and not nocase):
proc ::myswitch::exact { word body } {
uplevel 1 [list switch -exact -- $word $body]
}
proc ::myswitch::glob { word body } {
uplevel 1 [list switch -glob -- $word $body]
}
proc ::myswitch::regexp { word body } {
uplevel 1 [list switch -regexp -- $word $body]
}
The nocase variety does the same thing in the ::myswitchnc namespace.
I know this is ugly, probably doesn't work in every case, the point is
illustration of the complexity, and what it would take to reduce this
to the 'no option parsing' case.
With 8.5, you add the following subcommands:
proc ::myswitch::regmvar { matchvar word body } {
uplevel 1 [list switch -regexp -matchvar $matchvar -- $word $body]
}
proc ::myswitch::regivar { indexvar word body } {
uplevel 1 [list switch -regexp -matchindex $indexvar -- $word
$body]
}
proc ::myswitch::regmivar { matchvar indexvar word body } {
uplevel 1 [list switch -regexp -matchvar $matchvar -indexvar
$indexvar -- $word $body]
}
So the number of subcommands has doubled from six to 12, and also
reduced the opportunities for compiling code.
The ensemble command:
namespace eval ::myswitch {
namespace export exact glob regexp regmvar regivar regmivar
namespace ensemble create
}
.
- Follow-Ups:
- Re: Tcl Switch case
- From: Donal K. Fellows
- Re: Tcl Switch case
- References:
- Tcl Switch case
- From: vinayak.mdesai@xxxxxxxxx
- Re: Tcl Switch case
- From: Andreas Leitgeb
- Re: Tcl Switch case
- From: Mark Janssen
- Re: Tcl Switch case
- From: WL
- Re: Tcl Switch case
- From: Bryan Oakley
- Re: Tcl Switch case
- From: Donal K. Fellows
- Re: Tcl Switch case
- From: Hanz
- Re: Tcl Switch case
- From: Donal K. Fellows
- Re: Tcl Switch case
- From: tom.rmadilo
- Re: Tcl Switch case
- From: Bryan Oakley
- Tcl Switch case
- Prev by Date: Tcl 8.4.18 and Oratcl
- Next by Date: Re: Tcl 8.4.18 and Oratcl
- Previous by thread: Re: Tcl Switch case
- Next by thread: Re: Tcl Switch case
- Index(es):
Relevant Pages
|