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

use java.util.LinkedHashMap
use java.util.Iterator

rem ' Create and declare my Map
declare LinkedHashMap hm!
hm! = new LinkedHashMap()

rem ' Create three greetings, and insert them into the Map.
declare Greeting greeting!
greeting! = new Hello()
hm!.put(greeting!.getValue(), greeting!)

greeting! = new HowAreYou()
hm!.put(greeting!.getValue(), greeting!)

greeting! = new Goodbye()
hm!.put(greeting!.getValue(), greeting!)

while 1
    rem ' Iterate through all the greetings and print them to the screen
    declare Iterator iter!
    iter! = hm!.values().iterator()
    while (iter!.hasNext())
        rem ' Iterator.next() returns a java.lang.Object, we must cast it
        rem ' to a "Greeting" to assign it to greeting! .
        greeting! = cast(Greeting, iter!.next())
        rem ' Print out the greeting selection.
        print str(greeting!.getValue()) + ": " + greeting!.getGreeting()
    wend

    rem ' Get the user's choice
    input "Choose your greeting: ", choice

    rem ' Obtain the selected greeting from the Map
    rem ' We have to cast() again to obtain the Greeting object from the
    rem ' java.lang.Object returned by LinkedHashMap.get()
    greeting! = cast(Greeting, hm!.get(choice))

    if greeting! = null() then
        rem ' If no greeting with that number, we'll ask them to choose again
        print
        print "ERROR: Invalid choice!"
        print
    else
        rem ' Otherwise, print their chosen greeting
        print
        print "    " + greeting!.getGreeting()
        print

        rem ' Here we downcast to "Goodbye", if this is a Goodbye object.
        rem ' If so, we'll exit
        declare Goodbye goodbye!
        goodbye! = cast(Goodbye, greeting!,err=*endif)
        wait 1
        goodbye!.sayGoodbye()
    endif
wend

rem ' The Base class for all the greetings
class public Greeting
    field public BBjString Greeting$
    field public BBjInt Value%
 
    method protected Greeting(BBjString p_greeting$, BBjInt p_value%)
        #Greeting$ = p_greeting$
        #Value% = p_value%
    methodend
 
    rem ' private no-arg constructor
    method private Greeting()
    methodend
classend

rem ' The Hello greeting is choice #1, with a greeting of "Hello!"
class public Hello EXTENDS Greeting
    method public Hello()
        #super!("Hello!", 1)
    methodend
classend

rem ' The HowAreYou greeting is choice #2, with a greeting of "How are you?"
class public HowAreYou EXTENDS Greeting
    method public HowAreYou()
        #super!("How are you?", 2)
    methodend
classend

rem ' The Goodbye greeting is choice #3, with a greeting of "Goodbye!"
class public Goodbye EXTENDS Greeting
    method public Goodbye()
        #super!("Goodbye!", 3)
    methodend

    method public VOID sayGoodbye()
        release
    methodend
classend

Example 2 (BBj 18.0 and higher)

declare int[] intarray!
declare int@[] clientintarray!
declare Integer[] IntegerArray!
declare Integer@[] ClientIntegerArray!

intarray! = new int[10]
temp! = intarray!
intarray! = cast(int[],temp!)

clientintarray! = new int@[10]
temp! = clientintarray!
clientintarray! = cast(int@[],temp!)

IntegerArray! = new Integer[10]
temp! = IntegerArray!
IntegerArray! = cast(Integer[],temp!)
IntegerArray! = cast(java.lang.Integer[],temp!)

ClientIntegerArray! = new Integer@[10]
temp! = ClientIntegerArray!
ClientIntegerArray! = cast(Integer@[],temp!)
ClientIntegerArray! = cast(java.lang.Integer@[],temp!)

See Also

Type Checker Overview

DECLARE Verb

USE Verb

Functions - Alphabetical Listing