Skip to content
Positron edited this page Aug 11, 2017 · 3 revisions

Strong Types

When assigning a value to a variable, the type of the value must match the type of the variable.

The primitive types are: int, fixed, bool, and str.

raw type

To be compatible with ACS, the raw type is introduced. The raw type behaves like the int type, but has the following additional characteristics:

  • A value of raw type can be assigned to a variable of any primitive type, and a variable of raw type can accept a value of any primitive type.
  • In binary operations, when one operand is of raw type and the other is of primitive type, the operand of primitive type is implicitly casted to the raw type.

A value of primitive type gets implicitly casted to raw. In a namespace block qualified with strict, no such implicit casting occurs.

Casts

You can force the compiler to treat a value of one type as a value of another type:

strict namespace {

script "Main" open {
   Print( d: ( int ) "abc" );
   Print( s: ( str ) 0 );
}

}

Conversions

A value of one type can be converted to another type. A conversion looks like a function call: the type you want to convert to is the function name, and the value you want to convert is the argument.

Conversion to int type
Conversion Description
int( int value ) Returns value.
int( fixed value ) Returns the whole part of value as an integer.
int( bool value ) Returns 1 if value is true; otherwise, returns 0.
int( str value ) Not currently supported.
Conversion to fixed type
Conversion Description
fixed( int value ) Returns a fixed-point number whose whole part is value.
fixed( fixed value ) Returns value.
fixed( bool value ) Returns 1.0 if value is true; otherwise, returns 0.0.
fixed( str value ) Not currently supported.
Conversion to bool type
Conversion Description
bool( int value ) Returns false if value is 0.0; otherwise, returns true.
bool( fixed value ) Returns false if value is 0; otherwise, returns true.
bool( bool value ) Returns value.
bool( str value ) Returns false if value is the empty string (""); otherwise, returns true.
bool( reference value ) Returns false if value is the null reference; otherwise, returns true.
Conversion to str type
Conversion Description
str( int value ) Same as: StrParam( d: value )
str( fixed value ) Same as: StrParam( f: value )
str( bool value ) Returns "" if value is true; otherwise, returns "1".
str( str value ) Returns value.

Operations

In binary operations, the operands must be of the same type.

Operations of fixed type
Operation Result Type Description
left * right fixed Same as: FixedMul( left, right )
left / right fixed Same as: FixedDiv( left, right )
Operations of str type
Operation Result Type Description
left == right bool Same as: ( StrCmp( left, right ) == 0 )
left != right bool Same as: ( StrCmp( left, right ) != 0 )
left < right bool Same as: ( StrCmp( left, right ) < 0 )
left <= right bool Same as: ( StrCmp( left, right ) <= 0 )
left > right bool Same as: ( StrCmp( left, right ) > 0 )
left >= right bool Same as: ( StrCmp( left, right ) >= 0 )
left + right str Same as: StrParam( s: left, s: right )
left += right str Same as: ( left = StrParam( s: left, s: right ) )
string[ index ] int Same as: GetChar( string, index )
! string bool Same as : ( StrCmp( string, "" ) == 0 )
Operations of reference type
Operation Result Type Description
left == right bool Returns true if left and right refer to the same object; otherwise, returns false.
left != right bool Returns true if left and right refer to a different object; otherwise, returns false.
! operand bool Returns true if operand is the null reference; otherwise, returns false.
operand !! Reference See: From nullable to non-nullable.

Functions

The str type has a function called length(). This function returns the length of the string:

strict namespace {

script "Main" open {
   // Same as: Print( d: StrLen( "Hello, World!" ) );
   Print( d: "Hello, World!".length() ); // Output: 13
}

}

An array has a function called length(). This function returns the length of the array dimension:

strict namespace {

int a[] = { 1, 2, 3 };

script "Main" open {
   Print( d: a.length() ); // Output: 3
}

}