Swan Lake Beta2 is here.
import ballerina/io;

//`var` says that the type of the variable is from the type of expression
// used to initialize it.
var x = "str";

function printLines(string[] sv) {
    // Type inference with a `foreach` statement.
    foreach var s in sv {
        io:println(s);
    }

}

public function main() {
    string[] s = [x, x];
    printLines(s);

    // Infers `x` as the `MyClass` type.
    var x = new MyClass();

    // Infers the class for `new` as `MyClass`.
    MyClass y = new;

}

class MyClass {
    function foo() {

    }
}

Type Inference

Type inference is local and restricted to a single expression. Overuse of type inference can make the code harder to understand.

import ballerina/io;
var x = "str";

var says that the type of the variable is from the type of expression used to initialize it.

function printLines(string[] sv) {
    foreach var s in sv {
        io:println(s);
    }

Type inference with a foreach statement.

}
public function main() {
    string[] s = [x, x];
    printLines(s);
    var x = new MyClass();

Infers x as the MyClass type.

    MyClass y = new;

Infers the class for new as MyClass.

}
class MyClass {
    function foo() {
    }
}
bal run type_inference.bal
str
str
import ballerina/io;

//`var` says that the type of the variable is from the type of expression
// used to initialize it.
var x = "str";

function printLines(string[] sv) {
    // Type inference with a `foreach` statement.
    foreach var s in sv {
        io:println(s);
    }

}

public function main() {
    string[] s = [x, x];
    printLines(s);

    // Infers `x` as the `MyClass` type.
    var x = new MyClass();

    // Infers the class for `new` as `MyClass`.
    MyClass y = new;

}

class MyClass {
    function foo() {

    }
}

Type Inference

Type inference is local and restricted to a single expression. Overuse of type inference can make the code harder to understand.

import ballerina/io;
var x = "str";

var says that the type of the variable is from the type of expression used to initialize it.

function printLines(string[] sv) {
    foreach var s in sv {
        io:println(s);
    }

Type inference with a foreach statement.

}
public function main() {
    string[] s = [x, x];
    printLines(s);
    var x = new MyClass();

Infers x as the MyClass type.

    MyClass y = new;

Infers the class for new as MyClass.

}
class MyClass {
    function foo() {
    }
}
bal run type_inference.bal
str
str

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