Sujet : Re: a typeof command for debugging?
De : m.n.summerfield (at) *nospam* gmail.com (Mark Summerfield)
Groupes : comp.lang.tclDate : 22. Jun 2025, 12:42:53
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <1038q7t$glp8$1@dont-email.me>
References : 1 2 3 4
User-Agent : Pan/0.154 (Izium; 517acf4)
On Sun, 22 Jun 2025 13:08:53 +0200, Ralf Fassel wrote:
* Mark Summerfield <m.n.summerfield@gmail.com>
| I've incorporated that but I still have an actual bug. The catch
always | returns false (failed) so objects always get returned as
list_or_str. Yet, | if I use info object class directly it correctly
returns the class name.
| So clearly I'm doing something wrong in the catch.
>
| proc typeof x {
| # puts "\n[tcl::unsupported::representation $x]"
| if {![catch {[info object class $x] name}]} {
| return $name
This looks suspicious: you are calling
[info object class $x]
and whatever that returns, you use as a command to call with argument
'name',
and discard the result. The variable $name should not be set when you
hit the return.
Did you rather mean
catch {info object class $x} name
?
R'
Thank you, you are right. (I haven't got up to catch in the 2nd edition of
Ashok's book yet.)
With your correction I now get class names:
proc typeof x {
# puts "\n[tcl::unsupported::representation $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 match "value is a dict*" \
[tcl::unsupported::representation $x]]} {
return dict
}
if {[string is list -strict $x]} { return list_or_str }
return str
}
}
set i 55
set j 0d55
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 "j=$j typeof=[typeof $j]"
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]"
Output:
i=55 typeof=int
j=0d55 typeof=int
x=9.9 typeof=real
b=true typeof=bool
L=1 2 4 8 typeof=list_or_str
d=a 1 b 2 c 4 typeof=dict
s=red herrings typeof=list_or_str
t=long elephants € typeof=list_or_str
r=::oo::Obj24 typeof=::Range