【问题标题】:Using typeof in Xtend在 Xtend 中使用 typeof
【发布时间】:2015-08-08 18:21:59
【问题描述】:

这可能是 Xtend 上最愚蠢的问题。但它就在这里

我正在尝试将以下内容转换为 Xtend

if (obj instanceof Integer) {
    message.reply((Integer)obj);
}

我知道我需要使用typeof 函数。

我可以使用typeof 作为instanceof 的直接替代品吗?

【问题讨论】:

    标签: xtend


    【解决方案1】:

    typeof 是检索类对象的旧语法。如果您使用的是较新版本的 Xtend,则不再需要。除此之外,instanceof 语法与 Java 中的语法相同,但 cast 语句不同,因此您可以编写:

    if (obj instanceof Integer) {
        message.reply(obj as Integer)
    }
    

    由于支持 Xtend 2.5 自动投射,因此您甚至可以编写:

    if (obj instanceof Integer) {
        message.reply(obj) // obj is automatically casted to Integer
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    相关资源
    最近更新 更多