#37 is, as operators

brian Sun 29 Jan 2006

I've implemented support for the is and as operators. They work just like C#, with as being a shortcut for either casting or returning null.

5 is Int -> true
5 is Str -> false
5 as Int -> 5
5 as Str -> null

In fcode these map to the Is and As opcodes.

In the Java bytecode these map directly to the INSTANCEOF and CHECKCAST opcodes. However, for parameterized types they will need to map to a method call. The way I'm going to implement that is to check if the rhs is a parameterized type. If so then I will route to Type.isAssignableTo(), otherwise I will use the Java opcodes for performance.

andy Sat 23 Dec 2006

In the CLR, both the as and is operators are implemented with the same IL opcode isinst:

// For 'as' we only need one instr
isinst  <System.Type>

// For 'is' we need to check for null and convert to Bool
isinst  <System.Type>
ldnull
cgt.un
// call Fan.Sys.Bool.Make(bool v)

Login or Signup to reply.