import ballerina/io;

// A variable of type `any` can hold any value except an error.
any x = 1;

public function main() {
    // Can cast `any` to specific type.
    int n = <int>x;
    
    io:println(n);

    // Langlib lang.value module contains functions that apply to multiple basic types.
    // Can convert to string.
    string s = x.toString();

    io:println(s == "1");

    // Can test its type with the `is` operator.
    float f = x is int|float ? <float>x : 0.0;

    io:println(f);
}

Any Type

any means any value except an error. Equivalent to a union of all non-error basic types. Use any|error for absolutely any value. Langlib lang.value module contains functions that apply to multiple basic types.

import ballerina/io;
any x = 1;

A variable of type any can hold any value except an error.

public function main() {
    int n = <int>x;
    
    io:println(n);

Can cast any to specific type.

    string s = x.toString();

Langlib lang.value module contains functions that apply to multiple basic types. Can convert to string.

    io:println(s == "1");
    float f = x is int|float ? <float>x : 0.0;

Can test its type with the is operator.

    io:println(f);
}
bal run any_type.bal
1
true
1.0