Reputation: 777
Whether any Tcl Api exist which will tell the type of Tcl_Obj
whether it is string ,int or list
I want to check whether it is list so that i can put
{} over it
Upvotes: 0
Views: 529
Reputation: 137557
You're not supposed to do that. Tcl regards types as things that it is allowed to freely change behind the scenes, and makes no guarantee at all that what it is doing is what you expect.
That said, in Tcl 8.6 you can use tcl::unsupported::representation
to get a description of a value that includes the current type.
% 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 ""
(That last one surprised me.)
% 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"
At the C level, the types are in the nullable typePtr
field of the Tcl_Obj
structure. (The “pure string
” example above has a null typePtr
.) The typePtr
points to a static Tcl_ObjType
structure, which in turn has a name
field that should have a human readable type name in it. The types themselves are not normally exposed to third-party code, though they might be possible to look up using Tcl_GetObjType()
. Not all types are registered, by policy.
You should not make your code behave differently according to what type you receive. That is not the Tcl Way Of Doing Things. We really do mean this.
Upvotes: 3