forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceive.nula
More file actions
36 lines (33 loc) · 1.04 KB
/
Copy pathreceive.nula
File metadata and controls
36 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Receive — demonstrates selective receive for actor message handling.
//
// `receive` scans the actor's mailbox in FIFO order for the first message
// whose behavior matches any arm, binds that message's payload values to
// the arm's params, and evaluates the arm body. Non-matching messages are
// skipped and stay queued. When nothing matches, the legacy non-blocking
// fallback runs: pop the next message and yield its first payload value
// (nil when the mailbox is empty).
//
// Run with: nulang examples/receive.nula
actor Echo {
behavior respond() {
// Selective receive: wait-free scan for a `msg` message; binds the
// payload to `x`. Other queued messages are left in the mailbox.
receive {
msg(x) => x
}
}
behavior msg(x: Int) { x }
}
actor MainActor {
behavior start() {
// Spawn the echo actor and send it a message.
let echo = spawn Echo {}
echo ! respond();
unit
}
}
fn main() {
let main = spawn MainActor {}
main ! start();
unit
}