是否存在任何会告诉Tcl_Obj
类型的Tcl Api是字符串,整数还是列表
我想检查它是否是列表,以便我可以把
{}在它上面
答案 0 :(得分:3)
你不应该这样做。 Tcl认为类型是允许在幕后自由更改的东西,并不能保证什么是什么它正在做的就是你所期望的。
也就是说,在Tcl 8.6中,您可以使用tcl::unsupported::representation
来获取包含当前类型的值的描述。
% tcl::unsupported::representation [expr 1]
value is a int with a refcount of 1, object pointer at 0x100870c10, internal representation 0x1:0x0, no string representation
% tcl::unsupported::representation [expr 123456789123456789123456789]
value is a bignum with a refcount of 1, object pointer at 0x100871a20, internal representation 0x100882b90:0x20004, no string representation
% tcl::unsupported::representation [expr 1.5]
value is a double with a refcount of 1, object pointer at 0x1008713f0, internal representation 0x3ff8000000000000:0x100871420, no string representation
% tcl::unsupported::representation abc
value is a pure string with a refcount of 3, object pointer at 0x100874b10, string representation "abc"
% tcl::unsupported::representation [list a b c]
value is a list with a refcount of 3, object pointer at 0x100870e80, internal representation 0x100902f50:0x0, string representation "a b c"
% tcl::unsupported::representation [dict create a b c d]
value is a dict with a refcount of 3, object pointer at 0x1008717e0, internal representation 0x1008d4f10:0x0, string representation "a b c d"
% tcl::unsupported::representation [set s abc;string length $s;set s]
value is a string with a refcount of 5, object pointer at 0x100874b10, internal representation 0x1008a2ed0:0x1008710f0, string representation "abc"
% tcl::unsupported::representation {}
value is a bytecode with a refcount of 19, object pointer at 0x100870b50, internal representation 0x1008d2510:0x0, string representation ""
(最后一个让我感到惊讶。)
% tcl::unsupported::representation [set c stdin;gets $c;set c]
value is a channel with a refcount of 5, object pointer at 0x100871810, internal representation 0x1008b9a10:0x100829a10, string representation "stdin"
在C级别,类型位于typePtr
结构的可空 Tcl_Obj
字段中。 (上面的“pure string
”示例为空typePtr
。)typePtr
指向静态Tcl_ObjType
结构,而该结构又有一个name
字段应该有一个人类可读的类型名称。这些类型本身通常不会暴露给第三方代码,尽管可能可以使用Tcl_GetObjType()
进行查找。并非所有类型都按政策注册。
根据您收到的类型,您不应该使代码的行为有所不同。这不是Tcl的做事方式。我们确实是这个意思。