as operator

The as operator casts a value of one EGL type to another type. The most common use for the as operator is in conjunction with objects that Java™ returns through the ExternalType syntax. This access is strictly static; therefore, you need to cast objects to types that you have defined to EGL. For more information, see ExternalType part.

The operator is also useful in dealing with ANY type variables.

Syntax

Syntax diagram for the as operator
expression
Any valid EGL expression.
type
Any EGL type except DataItem. A DataItem part does not define a type separate from the primitive type it represents. In the case of ExternalType references, the runtime code needs to reference the actual type represented by the EGL ExternalType. The type can include the nullable type specification character "?" (see "Null values and the nullable type").

Foreign language casts

Use the as cast operator in situations where the mapping of EGL types to Java types is not sufficient (see Passing arguments to Java types). This is a special case of the as operator called a foreign language cast, in which the type is prefixed by a language name and a colon. The entire type operand is enclosed in quotation marks, as in the following:
null as "java:java.lang.Integer"
Because the Java objID type is not a primitive, EGL reverses the parts of the operand in this case:
myVar as "objID:java"
The following example shows a foreign language cast with the javaLib.store() method:
  javaLib.store("storeId" as "objID:java", "myId" as "objID:java",
      "myMethod", myVar as "java:smallfloat");

These foreign language casts are available only with EGL's Java access functions (see Java access functions). These are older functions that EGL maintains for compatibility with earlier versions. For new code, use the more powerful ExternalType syntax.

Example

In the following example, the default primitive type for a dictionary value is ANY. You cannot use the ANY type in a numeric expression, so you must cast the values as numeric types.

aDict Dictionary{ a = 1, b = 4 };
result int;
result = (aDict.a as INT) + (aDict.b as INT);

Error conditions

If you try to cast a value that is not appropriate to the target type, EGL throws a TypeCastException.


Feedback