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
[...]
shows you the last type that was used on that object (besides string):
(chris) 50 % set a [expr {23*5}]
115
(chris) 51 % tcl::unsupported::representation $a
value is a int with a refcount of 4, object pointer at 0x55d6b8f8b320, internal representation 0x73:(nil), string representation "115"
Christian
Am 21.06.25 um 13:16 schrieb Mark Summerfield:
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
[...]
I don't think you can do much better than that, since Tcl is a weakly
typed language (usually referred to as the "EIAS" principle). The only
other thing you can do is peek into the internal cached type, which
shows you the last type that was used on that object (besides string):
(chris) 50 % set a [expr {23*5}]
115 (chris) 51 % tcl::unsupported::representation $a value is a int with
a refcount of 4, object pointer at 0x55d6b8f8b320,
internal representation 0x73:(nil), string representation "115"
Christian
* 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'
I found that tcl::unsupported::representation says "pure string" for
strings and numbers and bools and lists; it only seems to distinguish
dicts (Tcl 9.0.1).
(chris) 51 % set b [dict a 1 b 2]
b 2
(chris) 52 % tcl::unsupported::representation $b
value is a dict with a refcount of 4, object pointer at 0x5576f6d86e30, internal representation 0x5576f6da2d60:(nil), string representation "b 2"
On 22/06/2025 21:22, Christian Gollwitzer wrote:
(chris) 51 % set b [dict a 1 b 2]
b 2
(chris) 52 % tcl::unsupported::representation $b
value is a dict with a refcount of 4, object pointer at
0x5576f6d86e30, internal representation 0x5576f6da2d60:(nil), string
representation "b 2"
Funny. While clearing up one point, you are confusing readers again on another point: It may not immediately be clear how b can end up being
the dictionary "b 2". The explanation is that the command [dict a 1 b 2]
is interpreted as [dict append 1 b 2], because there is only one dict subcommand that starts with "a". Additionally "dict append" will create
the dict if it doesn't yet exist. So this creates a dict in a variable called "1", with key "b" that has a value of "2". That dict is then also copied to the variable "b".
I suppose you actually intended to use [dict create a 1 b 2].
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,064 |
Nodes: | 10 (1 / 9) |
Uptime: | 168:23:45 |
Calls: | 13,692 |
Calls today: | 2 |
Files: | 186,936 |
D/L today: |
10,592 files (3,130M bytes) |
Messages: | 2,411,602 |