forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.nula
More file actions
46 lines (35 loc) · 989 Bytes
/
Copy pathchat.nula
File metadata and controls
46 lines (35 loc) · 989 Bytes
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
37
38
39
40
41
42
43
44
45
46
// Chat Room — actors send messages to each other.
// Demonstrates actor spawn, state, behaviors, and message passing.
// Run with: nulang examples/book/chat.nula
actor ChatRoom {
state messages: Int = 0
behavior broadcast(sender_name: String, text: String) {
self.messages = self.messages + 1
perform IO.print("[" + sender_name + "]: " + text)
}
behavior count() {
perform IO.print(
"Total messages: " + perform Int.to_string(self.messages)
)
}
}
actor ChatClient {
state room_ref = 0
behavior init(room: Int) {
self.room_ref = room
}
behavior say(name: String, text: String) {
self.room_ref ! broadcast(name, text)
}
}
fn main() {
let room = spawn ChatRoom {}
let alice = spawn ChatClient {}
let bob = spawn ChatClient {}
alice ! init(room)
bob ! init(room)
alice ! say("Alice", "Hello!")
bob ! say("Bob", "Hi Alice!")
room ! count()
0
}