Swan Lake Beta3 is here.
import ballerina/io;

// Defines a class called `File`.
class File {

    string path;
    string contents;
    // The `init()` method is used to initialize the `object`.
    // when creating a new `object`.
    function init(string p, string? c) returns error? {
        self.path = p;
        self.contents = check c.ensureType(string);
    }

}

public function main() returns error? {
    // `new` returns the newly-constructed `File` object.
    File f = check new File("test.txt", "Hello World");

    io:println(f.contents);
}

Init Return Type

init function has a return type, which must be a subtype of error?. If init returns (), then new returns the newly constructed object. If init returns an error, then new returns that error. If init does not specify a return type, then the return type defaults to () as usual, meaning that new will never return an error.

import ballerina/io;
class File {

Defines a class called File.

    string path;
    string contents;
    function init(string p, string? c) returns error? {
        self.path = p;
        self.contents = check c.ensureType(string);
    }

The init() method is used to initialize the object. when creating a new object.

}
public function main() returns error? {
    File f = check new File("test.txt", "Hello World");

new returns the newly-constructed File object.

    io:println(f.contents);
}
bal run init_return_type.bal
Hello World
import ballerina/io;

// Defines a class called `File`.
class File {

    string path;
    string contents;
    // The `init()` method is used to initialize the `object`.
    // when creating a new `object`.
    function init(string p, string? c) returns error? {
        self.path = p;
        self.contents = check c.ensureType(string);
    }

}

public function main() returns error? {
    // `new` returns the newly-constructed `File` object.
    File f = check new File("test.txt", "Hello World");

    io:println(f.contents);
}

Init Return Type

init function has a return type, which must be a subtype of error?. If init returns (), then new returns the newly constructed object. If init returns an error, then new returns that error. If init does not specify a return type, then the return type defaults to () as usual, meaning that new will never return an error.

import ballerina/io;
class File {

Defines a class called File.

    string path;
    string contents;
    function init(string p, string? c) returns error? {
        self.path = p;
        self.contents = check c.ensureType(string);
    }

The init() method is used to initialize the object. when creating a new object.

}
public function main() returns error? {
    File f = check new File("test.txt", "Hello World");

new returns the newly-constructed File object.

    io:println(f.contents);
}
bal run init_return_type.bal
Hello World
Subscribe to the newsletter

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