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