import ballerina/io;function main (string[] args) {
    float f = 10.0;
    var i = <int>f;
    io:println(i);
    int intVal = 45;
    var strVal = <string>intVal;
    strVal = "Sri Lanka";
    var intResult = <int>strVal;
    match intResult{
        int value =>{
            io:println(value);
        }
        error err =>{
            io:println("error: " + err.message);
        }
    }
    strVal = "5";
    var stringResult = <int> strVal;
    match stringResult{
        int value =>{
            io:println(value);
        }
        error err =>{
            io:println("Error occurred while casting" + err.message);
        }
    }
    boolean boolVal = true;
    intVal = <int>boolVal;
    io:println(intVal);
    intVal = -10;
    boolVal = <boolean> intVal;
    io:println(boolVal);
    strVal = "true";
    boolVal = <boolean>strVal;
    io:println(boolVal);
}

Type Conversion

Type conversion transforms the value so that it can be interpreted as belonging to another type. There are safe and unsafe conversions. For unsafe conversions, Ballerina compiler will enforce you to use multi-return conversion expression.

import ballerina/io;
function main (string[] args) {
    float f = 10.0;
    var i = <int>f;
    io:println(i);

A ‘float’ to ‘int’ conversion is considered as a type conversion in Ballerina because the underlying representation of the value changes with this conversion.

    int intVal = 45;
    var strVal = <string>intVal;

An ‘int’ to ‘string’ conversion is always safe.

    strVal = "Sri Lanka";
    var intResult = <int>strVal;
    match intResult{
        int value =>{
            io:println(value);
        }
        error err =>{
            io:println("error: " + err.message);
        }
    }

A ‘string’ to ‘int’ conversion is not always safe. Therefore we consider it as an unsafe conversion. The compiler enforces use of the multi-return conversion expression as follows. A TypeConversionError represents an error that occurs during a TypeConversion.

    strVal = "5";
    var stringResult = <int> strVal;
    match stringResult{
        int value =>{
            io:println(value);
        }
        error err =>{
            io:println("Error occurred while casting" + err.message);
        }
    }

If you know that this conversion will always be successful, you can ignore the error as follows.

    boolean boolVal = true;
    intVal = <int>boolVal;
    io:println(intVal);

A ‘boolean’ to ‘int’ conversion is always safe. You get 0 for a ‘false’ value and 1 for a ‘true’ value.

    intVal = -10;
    boolVal = <boolean> intVal;
    io:println(boolVal);

This is an ‘int’ to ‘boolean’ conversion. The boolean value will be ‘false’ only if the int value is 0.

    strVal = "true";
    boolVal = <boolean>strVal;
    io:println(boolVal);
}

This is a ‘string’ to ‘boolean’ conversion.

$ ballerina run type-conversion.bal
10
error: 'string' cannot be converted to 'int'
5
1
true
true