2013-02-28 83 views

回答

2

您可以精確地確定一個值是手柄的目的是通過使用info object isa object

if {[info object isa object $thing]} { 
    puts "Hey, $thing is an object!" 
} 

一般來說雖然,TCL的類型系統具有的所有值至少是名義上字符串。更嚴格地說,每個值都可串行化爲一個字符串。有些值也具有其他特性(例如,數字也知道它們的數字性)。 TclOO對象句柄是字符串和命令名稱(因此可以是rename d)和(當然)對象句柄。

0

愚蠢的答案:

# Since a normal string is unlikly to be "::oo::object", this will return 1 
# if the argument is not ::oo::object 
proc is_oo_object args { 
    string equals $arg ::oo::object 
} 

# gettype - higly accurate 
proc gettype arg { 
    # EIAS 
    return "string" 
} 

答案很簡單:你不能。如果有人向你傳遞一個對象的名字,那就是一個字符串。 (見的Tcl/Tk維基EIAS

你可以嘗試猜測,如果它是一個面向對象:: ::對象,如果你檢查是否存在使用該名稱的命令:

if {[llength [namespace which $arg]]} { 
    .... 
} 

這仍然不意味着這是:: oo :: object。 你可以試着用expr {[catch {info object class $arg ::oo::object} res] && $res}來檢查它,但是誰告訴你有人想要通過oo::class作爲字符串?