Swan Lake Beta2 is here.
import ballerina/io;

// Defines a class called `EvenNumberGenerator`.
// Each class has its own `next()` method, which gets 
// invoked when the stream's `next()` function
// gets called.
class EvenNumberGenerator {
    int i = 0;
    public isolated function next() returns record {| int value; |}|error? {
        self.i += 2;
        return { value: self.i };
    }
}

type ResultValue record {|
    int value;
|};

public function main() {
    EvenNumberGenerator evenGen = new();
    // Creates a `stream` passing an `EvenNumberGenerator` object
    // to the `stream` constructor.
    stream<int, error?> evenNumberStream = new(evenGen);

    var evenNumber = evenNumberStream.next();
    
    if (evenNumber is ResultValue) {
        io:println("Retrieved even number: ", evenNumber.value);
    }
}

Stream Type

A stream represents a sequence of values that are generated as needed. The end of a stream is indicated with a termination value, which is error or nil. The type stream<T,E> is a stream where the members of the sequence are type T and termination value is type E. stream<T> means stream<T,()>. The stream type is a separate basic type, but like an object.

import ballerina/io;
class EvenNumberGenerator {
    int i = 0;
    public isolated function next() returns record {| int value; |}|error? {
        self.i += 2;
        return { value: self.i };
    }
}

Defines a class called EvenNumberGenerator. Each class has its own next() method, which gets invoked when the stream’s next() function gets called.

type ResultValue record {|
    int value;
|};
public function main() {
    EvenNumberGenerator evenGen = new();
    stream<int, error?> evenNumberStream = new(evenGen);

Creates a stream passing an EvenNumberGenerator object to the stream constructor.

    var evenNumber = evenNumberStream.next();
    
    if (evenNumber is ResultValue) {
        io:println("Retrieved even number: ", evenNumber.value);
    }
}
bal run stream_type.bal
Retrieved even number: 2
import ballerina/io;

// Defines a class called `EvenNumberGenerator`.
// Each class has its own `next()` method, which gets 
// invoked when the stream's `next()` function
// gets called.
class EvenNumberGenerator {
    int i = 0;
    public isolated function next() returns record {| int value; |}|error? {
        self.i += 2;
        return { value: self.i };
    }
}

type ResultValue record {|
    int value;
|};

public function main() {
    EvenNumberGenerator evenGen = new();
    // Creates a `stream` passing an `EvenNumberGenerator` object
    // to the `stream` constructor.
    stream<int, error?> evenNumberStream = new(evenGen);

    var evenNumber = evenNumberStream.next();
    
    if (evenNumber is ResultValue) {
        io:println("Retrieved even number: ", evenNumber.value);
    }
}

Stream Type

A stream represents a sequence of values that are generated as needed. The end of a stream is indicated with a termination value, which is error or nil. The type stream<T,E> is a stream where the members of the sequence are type T and termination value is type E. stream<T> means stream<T,()>. The stream type is a separate basic type, but like an object.

import ballerina/io;
class EvenNumberGenerator {
    int i = 0;
    public isolated function next() returns record {| int value; |}|error? {
        self.i += 2;
        return { value: self.i };
    }
}

Defines a class called EvenNumberGenerator. Each class has its own next() method, which gets invoked when the stream’s next() function gets called.

type ResultValue record {|
    int value;
|};
public function main() {
    EvenNumberGenerator evenGen = new();
    stream<int, error?> evenNumberStream = new(evenGen);

Creates a stream passing an EvenNumberGenerator object to the stream constructor.

    var evenNumber = evenNumberStream.next();
    
    if (evenNumber is ResultValue) {
        io:println("Retrieved even number: ", evenNumber.value);
    }
}
bal run stream_type.bal
Retrieved even number: 2

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