CAST() Function
Syntax
CAST(type, object{,ERR=lineref})
Description
In BBj 6.0 and higher, the CAST() function takes a type name and an
object and returns an object of the provided type. This may not be the
same object if the CAST() function must perform a conversion.
Parameter |
Description |
---|---|
object |
The object to which a new type may be assigned. |
type |
A BBj Object type, Custom Object type, or a Java class or primitive. Examples of BBj Objects include BBjString and BBjDataBoundGrid. Custom Objects are those created using the CLASS verb. Custom Objects must either be defined in the same program file, fully qualified, or locatable by a USE verb or config.bbx directive. Java types include classes such as String and java.util.HashMap. Java Objects must either be fully qualified or locatable by a USE verb or config.bbx directive. In BBj 8.0 and higher, an @ suffix following a type name indicates a ClientObject reference. In BBj 18.0 and higher, the CAST() function can specify a Java array object by appending one or more [] after a Java class or primitive type name. |
Every expression in BBj has a type that BBj determines from the source
code without regard to control flow. Expressions for which no type may
be statically determined have a special "undeclared" type. Developers
may use the DECLARE verb to attribute types to specific variables, in
which case a variable will always contain a reference to an object that
may be assigned to the type to which it is declared. BBj will also assign
a static type to expressions for which BBj may derive the static type
from programmatically declared variables and return types of methods called
on and with other statically typed expressions. Variables that are DECLARE'd
provide type safety, but also enforce a requirement that methods called
on them are statically typed.
CAST() may help in legacy code that does not make use of the static type system in BBj. If a sequence of code does not use DECLARE, but the developer wishes to make use of the type checker and strong type-checking facilities in BBj, the developer may use the CAST() function to provide an assertion to BBj that the given value is of the appropriate type. This provides a way for developers to 'mix-and-match' their use of Custom Objects.
For Java reference types (subclasses of java.lang.Object), the CAST() function will allow reference widening conversions as defined by the Java Language Specification. This means CAST() can "downcast" an object statically declared as an interface to its implementation type, for instance, or it can cast an object for which BBj determines its static type to be one class, but the object is actually a subclass of that class.
CAST() is not currently well-defined on BBj core types. BASIS recommends avoiding CAST() on objects of type BBjString, BBjNumber or BBjInt, except when casting an object whose static type is java.lang.Object or undeclared. Conversions on these objects are not generally predictable.
Example 1
|
Example 2 (BBj 18.0 and higher)
|