Type Conversion in SIL

This document covers automatic type conversion rules and behavior, distinct from explicit type conversion using the cast operator.

Basic Type Conversion Rules

Type conversion in SIL allows variables of different types to be automatically converted when using the assignment (=) operator. Type conversion occurs when you assign a value of one type to a variable of another type. The system attempts to convert the source value into a format that matches the target type while preserving the semantic meaning of the data.

To ensure correct transformation, improve code clarity and prevent unexpected behavior, make sure to:

  • Use appropriate types from the start.

  • Verify that source types can be converted to target types.

  • Verify conversion behavior with different input types.

  • For complex cases, use explicit conversions instead of relying on the automatic conversion functionality.

  • Implement proper error handling for conversion failures. This includes providing meaningful error messages.

  • Use consistent formatting in all your scripts.

All conversion functionality is backward compatible with versions prior to v2.5.

Basic Type Conversion Matrix

This table shows all possible conversions between types:

Target Type ↓ 

Source Type →

Additional information, notes, and examples

string*

number

integer

boolean

date

interval

string

Universal conversion target.

number

  • "1" → 1.0

  • 1 → 1.0

  • Strings must contain valid numeric values.

integer

  • "2" → 2

  • 2.5 → 2

  • Numbers are truncated, not rounded.

boolean

  • "true" → true

  • 1 → true

  • 0 → false

  • Non-zero numbers convert to true.

date

  • "2024-01-01" → 2024-01-01

  • 1704067200000 → 2024-01-01

  • When converting a number to a date, the number is interpreted as milliseconds since Unix epoch.

interval

  • "1h" → interval

  • 3600000 → "1h"

  • "60m" → "1h"

  • When converting a number to an interval, the number is interpreted as a duration in milliseconds. This represents a time span rather than a specific point in time.

*The conversion from string to a target type is done by parsing the string as a text representation of the target type.

Conversion Examples

// Number conversion examples number n1 = "42"; // String to number: 42.0 number n2 = 42; // Integer to number: 42.0 // Integer conversion examples integer i1 = "123"; // String to integer: 123 integer i2 = 123.7; // Number to integer: 123 (truncated) // Boolean conversion examples boolean b1 = "true"; // String to boolean: true boolean b2 = 1; // Number to boolean: true boolean b3 = 0; // Number to boolean: false // Date conversion examples date d1 = "2024-01-01"; // String to date date d2 = 1704067200000; // Milliseconds to date // Interval conversion examples interval iv1 = "1h 30m"; // String to interval interval iv2 = 5400000; // Milliseconds to interval

When converting a string to an array type, the string is first transformed to a string[] by splitting it at every "|" character, and then the conversion is done from string []  to the target type array.

Advanced Type Conversions

Array Conversion Rules

Array conversion is possible if the following conditions are met:

  • The element types (inner type of the array) are convertible.

  • The conversion rules for array elements follow the same rules as single-value conversions explained earlier in this document.

  • All elements in the source array must be convertible to the target type.

Array Conversion Examples

You can cast a number[] to a string[] because number is convertible to string (Example 1). However you cannot convert a date[] to an interval[] because date cannot be converted to interval (Example 2).

// Example 1: Number Array to String Array (Valid) number[] numbers = [1, 2.5, 3.7, -4.2]; string[] stringNumbers = numbers; // Valid conversion // Result: ["1", "2.5", "3.7", "-4.2"] // Example 2: Date Array to Interval Array (Invalid) date[] dates = [ "2024-01-01", "2024-01-02", "2024-01-03" ]; // interval[] periods = dates; // This would cause a conversion error // Error: Cannot convert date to interval

String to Array Type Conversion Rules

When converting a string to an array, the process happens in two steps:

Step 1:

string → string[] and split on "|"

The string is first transformed to a string[] by splitting it at every "|" character.

Step 2:

string[] → target_type[]

The conversion is done from string[] to the target type array.

String Conversion Examples

// Step 1: "1|2|3" → ["1", "2", "3"] // Step 2: ["1", "2", "3"] → [1, 2, 3] // Basic number array conversion string numericString = "1|2|3|4|5"; number[] numbers = numericString; // Result: [1.0, 2.0, 3.0, 4.0, 5.0] // Basic boolean array conversion string boolString = "true|false|true|true"; boolean[] flags = boolString; // Result: [true, false, true, true]

Structure Conversion Rules

Structures can only be converted directly to string or array types. It may be possible for certain structures to be convertible to other types by using one or more intermediate types.

Structure to String Conversion

Conversions of this type follow these rules:

  • All fields are first converted to strings.

  • Fields are joined with commas.

Casting a structure to a string is equivalent to casting to a string[], and then the resulting array to string.

Structure to Array Conversion

Conversions of this type follow these rules:

  • All structure fields must be convertible to the array's element type.

  • Fields are converted in order of declaration.

Complex Conversions

Complex conversions (to types other than string/array) require:

  • Explicit intermediate steps

  • Manual casting between types

  • Fields that can ultimately convert to target type

Common common conversion patterns include:

  • Structure → string → target type

  • Structure → array → target type

In this example, a structure containing a single number field can be converted to a number by first casting it to a string and then the resulting string to a number.

Automatic Parameter Type Conversion

When calling functions (routines or UDRs), parameters are automatically converted to match the function’s expected parameter types, following the standard type conversion rules.