CAST
The CAST
operator
converts a Java object to the user-defined type or Java class specified.
Syntax
CAST( obj AS type )
- obj is a Java object.
- type is a user-defined type or a Java class.
Usage
The CAST()
operator is required when you want to assign an object reference
to variable defined with a user-defined type or Java class which requires narrowing
reference conversion.
Example
In this example, when assigning a
java.lang.StringBuffer
reference to a
java.lang.Object
variable, widening reference conversion occurs and no
CAST()
operator is needed, but when assigning an java.lang.Object
reference to a java.lang.StringBuffer
variable, you must cast the object reference
to a
java.lang.StringBuffer
:IMPORT JAVA java.lang.Object
IMPORT JAVA java.lang.StringBuffer
MAIN
DEFINE sb1, sb2 java.lang.StringBuffer
DEFINE o java.lang.Object
LET sb1 = StringBuffer.create()
LET o = sb1 -- Widening Reference Conversion does not need CAST()
LET sb2 = CAST( o AS java.lang.StringBuffer ) -- Narrowing
-- Reference Conversion needs CAST()
END MAIN
In order to cast an
fgl.FglRecord
object to a regular RECORD
,
you need to specify a user-defined type (TYPE
definition):IMPORT JAVA com.fourjs.fgl.lang.FglRecord
TYPE mytype RECORD f1, f2 INTEGER END RECORD
MAIN
DEFINE r mytype
DEFINE jr fgl.FglRecord
LET jr = r
LET r = CAST(jr AS mytype)
-- This is denied:
-- CAST(jr AS RECORD f1, f2 INTEGER END RECORD)
END MAIN