1

如何区分基本类型(如字符串)和 TclOO 中的 ::oo::object?

4

2 回答 2

2

您可以使用以下方法精确确定值是否是对象的句柄info object isa object

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

Generally speaking though, Tcl's type system has all values being at least nominally strings. More strictly, every value is serializable to a string. Some values have other natures as well (e.g., numbers know about their numeric-ness as well). TclOO object handles are strings, and command names (and hence can be renamed), and (of course) object handles.

于 2013-03-01T07:21:27.657 回答
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"
}

简单的答案:你不能。如果有人将对象的名称传递给您,则它是一个字符串。(参见EIAS的 Tcl/Tk wiki )

如果您检查是否存在具有该名称的命令,您可以尝试猜测它是否是 ::oo::object:

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

这仍然不意味着这是一个 ::oo::object。您可以尝试使用 来检查它expr {[catch {info object class $arg ::oo::object} res] && $res},但谁告诉您有人想oo::class作为字符串传递?

于 2013-02-28T12:59:50.213 回答