0

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?

David Laberge
  • 15,435
  • 14
  • 53
  • 83

1 Answers1

4

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
}
Franz Becker
  • 705
  • 5
  • 8
  • 1
    The switch expression also has a nice syntax for automatic type conversion, named "type guards": http://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#type-guards – snorbi Feb 09 '16 at 14:52