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在內部使用相當多的不同類型(例如,,緩存的命令和變量名稱),在正常情況下你不想探測;事情是引擎蓋下相當複雜的...
你需要什麼版本的TCL? –
你能解釋一下爲什麼你需要這個嗎?我的意思是,因爲Tcl本質上是一種無類型的語言,所以你問的問題看起來像是在尋求麻煩。 – kostix
@Kostix,當然。我需要的原因是創建一個將字典解析爲JSON的過程。例如在JSON中的字符串被「」包圍,而整數不是。另外,如果字典要包含另一個字典,那麼該字典應該在JSON對象中獲得它自己的JSON對象。 – Tom