Search code examples
xtend

Using typeof in Xtend


It might be the stupidest question on Xtend. But here it goes

I'm trying to convert the following to Xtend

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

I know that I would need to use the typeof function.

Can I use typeofas a direct replacement to instanceof?


Solution

  • typeof is an old syntax to retrieve the class object. If you're using a newer version of Xtend this is no longer required. Apart from that the instanceof syntax is just like in Java but the cast statement is different so you would write:

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

    Since Xtend 2.5 auto-casting is supported so you can even write:

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