Tcl的变量没有类型(除了他们是否真的变量的关联数组 - 即使用$foo(bar)语法 - FO r你使用array exists)但Tcl的值。好吧,有点。 Tcl可以在不同类型之间进行变异,因为它认为合适,并且不公开这些信息[*];你所能做的就是检查一个值是否符合特定的类型。
这种一致性检查与完成string is(在您需要的-strict选项,丑陋的历史原因):
if {[string is integer -strict $foo]} {
puts "$foo is an integer!"
}
if {[string is list $foo]} { # Only [string is] where -strict has no effect
puts "$foo is a list! (length: [llength $foo])"
if {[llength $foo]&1 == 0} {
# All dictionaries conform to lists with even length
puts "$foo is a dictionary! (entries: [dict size $foo])"
}
}
请注意,所有值符合字符串类型; Tcl的值是总是可序列化。对于JSON序列化,可以使用肮脏的黑客来产生一个“正确的”序列化(严格地说,从Tcl的角度来看,所有东西都是正确的,但这对其他语言并不完全有帮助)与Tcl 8.6。代码要做到这一点,原本张贴在Rosetta Code是:
package require Tcl 8.6
proc tcl2json value {
# Guess the type of the value; deep *UNSUPPORTED* magic!
regexp {^value is a (.*?) with a refcount} \
[::tcl::unsupported::representation $value] -> type
switch $type {
string {
# Skip to the mapping code at the bottom
}
dict {
set result "{"
set pfx ""
dict for {k v} $value {
append result $pfx [tcl2json $k] ": " [tcl2json $v]
set pfx ", "
}
return [append result "}"]
}
list {
set result "\["
set pfx ""
foreach v $value {
append result $pfx [tcl2json $v]
set pfx ", "
}
return [append result "\]"]
}
int - double {
return [expr {$value}]
}
booleanString {
return [expr {$value ? "true" : "false"}]
}
default {
# Some other type; do some guessing...
if {$value eq "null"} {
# Tcl has *no* null value at all; empty strings are semantically
# different and absent variables aren't values. So cheat!
return $value
} elseif {[string is integer -strict $value]} {
return [expr {$value}]
} elseif {[string is double -strict $value]} {
return [expr {$value}]
} elseif {[string is boolean -strict $value]} {
return [expr {$value ? "true" : "false"}]
}
}
}
# For simplicity, all "bad" characters are mapped to \u... substitutions
set mapped [subst -novariables [regsub -all {[][\u0000-\u001f\\""]} \
$value {[format "\\\\u%04x" [scan {& } %c]]}]]
return "\"$mapped\""
}
警告:不支持上面的代码。这取决于肮脏的黑客。它很容易在没有预警的情况下突破。 (但是它不工作。移植到Tcl的8.5将需要一个微小的C扩展到读出的类型的注释。)
[*]严格,但它用于发现当前类型注释提供一个不支持的接口的价值为8.6 - 作为::tcl::unsupported::representation的一部分 - 但该信息采用刻意的人类可读形式,如有更改,恕不另行通知。它用于调试,而不是代码。而且,Tcl在内部使用相当多的不同类型(例如,,缓存的命令和变量名称),在正常情况下你不想探测;事情是引擎盖下相当复杂的...