Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Integer option to switch command

Closed / Fixed1106138fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 20 days ago
Descriptionplain2005-01-20 17:26:11
I propose the addition of an option to the switch command to 
allow integer comparisons much like the switch statement in C.
dkfplain2005-01-21 15:56:19
Logged In: YES 
user_id=79902

Matter was discussed on c.l.t:
http://groups-beta.google.com/group/comp.lang.tcl/browse_frm/thread/a0b1398ff0b42638/66809fe88969cd04?_done=%2Fgroup%2Fcomp.lang.tcl%3Fhl%3Den%26&_doneTitle=Back+to+topics&_doneTitle=Back&&d#66809fe88969cd04

Idea is that this should work as (arithmetically) expected:
  switch -integer [expr {int(sqrt(4))}] {
     0x00 { puts "root 4 is zero" }
     0x01 { puts "root 4 is one" }
     0x02 { puts "root 4 is two" }
  }

i.e. We want an option for [switch] to use ==-like equality
as well as eq-like equality. For the bytecode-compiled
version, this is almost trivial (add check-integer and use
INST_EQ instead of INST_STR_EQ) but it is a bit more work
for the non-BC version.
serhiy.storchakawiki2026-11-13 11:18:43

Implemented in Tcl 9.1 by TIP 730: switch -integer compares the value and the patterns as integers, so the example above matches 0x02.

Log in (anonymous is fine) to add a comment.