forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_actors.nula
More file actions
74 lines (60 loc) · 1.86 KB
/
Copy path08_actors.nula
File metadata and controls
74 lines (60 loc) · 1.86 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 08_actors.nula - Actors: spawn, send, and behaviors
// Demonstrates: actor declaration, state, behaviors, spawn,
// message passing with !, actors printing from behaviors
//
// Run: nulang examples/08_actors.nula
// A counter actor that tracks and displays its count
actor Counter {
state count: Int = 0
behavior increment(by: Int) {
self.count = self.count + by
}
behavior show() {
perform IO.print(" Counter value: " + perform Int.to_string(self.count))
}
}
// A greeter actor with configurable greeting and name
actor Greeter {
state greeting: String = "Hello"
state name: String = "World"
behavior set_greeting(g: String) {
self.greeting = g
}
behavior set_name(n: String) {
self.name = n
}
behavior greet() {
perform IO.print(" " + self.greeting + ", " + self.name + "!")
}
}
// A calculator actor that prints results
actor Calculator {
state last: Int = 0
behavior add(x: Int, y: Int) {
let result = x + y
self.last = result
perform IO.print(" " + perform Int.to_string(x) + " + " + perform Int.to_string(y) + " = " + perform Int.to_string(result))
}
behavior multiply(x: Int, y: Int) {
let result = x * y
self.last = result
perform IO.print(" " + perform Int.to_string(x) + " * " + perform Int.to_string(y) + " = " + perform Int.to_string(result))
}
}
fn main() {
perform IO.print("Counter actor:")
let counter = spawn Counter {}
counter ! increment(5)
counter ! increment(3)
counter ! show()
perform IO.print("Greeter actor:")
let greeter = spawn Greeter {}
greeter ! greet()
greeter ! set_name("Actor System")
greeter ! greet()
perform IO.print("Calculator actor:")
let calc = spawn Calculator {}
calc ! add(40, 2)
calc ! multiply(6, 7)
0
}