Type conversions

Type conversion is a technique in wich variables of different types can be assigned to each other. There are two types of type conversions implicit and explicit. Implicit conversions are handled automatically by the compiler or runtime engine and explicit conversions are handled by the developer.

Basic scalars

The following table describes how the conversion of basic scalars works:

Source

Destination

Conversion method

Conversion test

Conversion errors

How the conversion is done

Source

Destination

Conversion method

Conversion test

Conversion errors

How the conversion is done

int

int

-

-

-



int

decimal

automatic

-

-



int

datetime

-

-

-



int

string

automatic

-

-

10 => "10"

int

boolean

automatic

-

-

1 => true; value != 0 => false

decimal

int

int->fromDecimal

-

-

Returns the integer part. Any fractional part of this decimal will be discarded. No rounding. 

decimal

decimal

-

-

-



decimal

datetime

-

-

-



decimal

string

string->fromDecimal

-

-

See java documentation for BigDecimal.toString method

decimal

boolean

automatic

-

-

1 => true; value != 0 => false

datetime

int

-

-

-



datetime

decimal

-

-

-



datetime

datetime

-

-

-



datetime

string

string->fromDateTime

-

-

The date in ISO8601 format.

datetime

boolean

-

-

-



string

int

int->fromString

string->isInt

yes



string

decimal

decimal->fromString

string->isDecimal

yes



string

datetime

datetime->fromString

string->isDateTime

yes

true => if the string is a valid ISO8601 date, false otherwise

string

string

-

-

-



string

boolean

bool->fromString

-

-



bool

int

automatic

-

-

true => 1; false => 0

bool

decimal

automatic

-

-

true => 1; false => 0

bool

datetime

-

-

-



bool

string

automatic

-

-

true => "true"; false => "false"

bool

boolean

-

-

-



Complex scalars

Complex scalars can be implicitly converted by the compiler or runtime engine only if all the properties of the destination type are found in the source type with the same name and type. The following table exemplifies all the valid implicit conversions:

Source

Destination

Conversion method

How the conversion is done

Source

Destination

Conversion method

How the conversion is done

type src {
m1 as string
m2 as int
m3 as boolean
}

type dest {
m1 as string
m2 as int
m3 as boolean
}

automatic

Properties m1, m2, m3 from src will be set in dest .

type src {
m1 as string
m2 as int
m3 as boolean
m4 as decimal
}

type dest {
m1 as string
m2 as int
m3 as boolean
}

automatic

Properties m1, m2, m3 from src will be set in dest except for m4 which will be ignored.

type src {
m1 as string
m2 as int
m3 as boolean
}

type dest {
m1 as string
m2 as int
m3 as boolean
m4 as decimal
}

-

Conversion must be handled by the developer because dest type has more properties than src type.

type src {
m1 as string
m2 as int
m3 as boolean
}

type dest {
m4 as string
m5 as int
m6 as boolean
}

-

Conversion must be handled by the developer because dest type has different property names than src type.

type src {
m1 as string
m2 as int
m3 as boolean
}

type src {
m1 as boolean
m2 as string
m3 as int
}



Conversion must be handled by the developer because dest type has different property types than src type.