Swan Lake Beta2 is here.
import ballerina/io;

public function main() {

    worker A {
        // Use `-> W` to send a message to worker `W`.
        1 -> B;
        2 -> C;

    }

    worker B {
        // Use `<- W` to receive a message from worker `W`.
        int x1 = <- A;

        // Use `function` to refer to the function's default worker.
        x1 -> function;

    }

    worker C {
        int x2 = <- A;
        x2 -> function;
    }

    int y1 = <- B;
    int y2 = <- C;
    
    io:println(y1 + y2);
}

Inter-Worker Message Passing

Use -> W or <- W to send a message to or receive a message from worker W (use function to refer to the function’s default worker). Messages are copied using clone(); implies immutable values are passed without copy. Message sends and receives are paired up at compile-time. Each pair turns into horizontal line in the sequence diagram. Easy to use and safe, but limited expressiveness.

import ballerina/io;
public function main() {
    worker A {
        1 -> B;
        2 -> C;

Use -> W to send a message to worker W.

    }
    worker B {
        int x1 = <- A;

Use <- W to receive a message from worker W.

        x1 -> function;

Use function to refer to the function’s default worker.

    }
    worker C {
        int x2 = <- A;
        x2 -> function;
    }
    int y1 = <- B;
    int y2 = <- C;
    
    io:println(y1 + y2);
}
bal run inter_worker_message_passing.bal
3
import ballerina/io;

public function main() {

    worker A {
        // Use `-> W` to send a message to worker `W`.
        1 -> B;
        2 -> C;

    }

    worker B {
        // Use `<- W` to receive a message from worker `W`.
        int x1 = <- A;

        // Use `function` to refer to the function's default worker.
        x1 -> function;

    }

    worker C {
        int x2 = <- A;
        x2 -> function;
    }

    int y1 = <- B;
    int y2 = <- C;
    
    io:println(y1 + y2);
}

Inter-Worker Message Passing

Use -> W or <- W to send a message to or receive a message from worker W (use function to refer to the function’s default worker). Messages are copied using clone(); implies immutable values are passed without copy. Message sends and receives are paired up at compile-time. Each pair turns into horizontal line in the sequence diagram. Easy to use and safe, but limited expressiveness.

import ballerina/io;
public function main() {
    worker A {
        1 -> B;
        2 -> C;

Use -> W to send a message to worker W.

    }
    worker B {
        int x1 = <- A;

Use <- W to receive a message from worker W.

        x1 -> function;

Use function to refer to the function’s default worker.

    }
    worker C {
        int x2 = <- A;
        x2 -> function;
    }
    int y1 = <- B;
    int y2 = <- C;
    
    io:println(y1 + y2);
}
bal run inter_worker_message_passing.bal
3

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