Sujet : a typeof command for debugging?
De : m.n.summerfield (at) *nospam* gmail.com (Mark Summerfield)
Groupes : comp.lang.tclDate : 21. Jun 2025, 12:16:18
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <10364a2$11i1c$1@dont-email.me>
User-Agent : Pan/0.149 (Bellevue; 4c157ba)
I'd like a `typeof` command for debugging.
I've had a go but only bits of it work.
```
proc typeof x {
if {![catch {[info object class $x] name}]} {
return $name
} else {
if {[string is boolean -strict $x]} {
return bool
}
if {[string is integer -strict $x]} {
return int
}
if {[string is double -strict $x]} {
return real
}
if {[string is dict -strict $x]} {
return dict
}
if {[string is list -strict $x]} {
return list
}
return str
}
}
::oo::class create Range {
variable max
variable current
constructor max_ { my reset $max_ }
method reset {{max_ -1}} {
if {$max_ != -1} { set max $max_ }
set current -1
}
method next varName {
upvar $varName var
incr current
set var $current
expr { $var < $max }
}
}
set i 55
set x 9.9
set b true
set L {1 2 4 8}
set d [dict create a 1 b 2 c 4]
set s "red\therrings"
set t "long\telephants €"
set r [Range new 5]
puts "i=$i typeof=[typeof $i]"
puts "x=$x typeof=[typeof $x]"
puts "b=$b typeof=[typeof $b]"
puts "L=$L typeof=[typeof $L]"
puts "d=$d typeof=[typeof $d]"
puts "s=$s typeof=[typeof $s]"
puts "t=$t typeof=[typeof $t]"
puts "r=$r typeof=[typeof $r]"
puts "r=$r info object class=[info object class $r]"
```
Here's the output:
```
i=55 typeof=int
x=9.9 typeof=real
b=true typeof=bool
L=1 2 4 8 typeof=dict
d=a 1 b 2 c 4 typeof=dict
s=red herrings typeof=dict
t=long elephants € typeof=list
r=::oo::Obj24 typeof=list
r=::oo::Obj24 info object class=::Range
```