Swan Lake Beta2 is here.
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
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

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand