-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-web-server-stable.err
More file actions
79 lines (68 loc) · 1.98 KB
/
Copy path05-web-server-stable.err
File metadata and controls
79 lines (68 loc) · 1.98 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
75
76
77
78
79
# SPDX-License-Identifier: MPL-2.0
# Example: Stable Web Server
# Learning: How to write high-stability async code
# Type definitions
type Request = {
user_id: Int,
path: String
}
type Response = {
status: Int,
body: String
}
# Pure function - no side effects
async function handle_request(req: Request) -> Response
# Parallel async - independent operations
let [user, posts] = await Promise.all([
fetch_user(req.user_id),
fetch_posts(req.user_id)
])
# Pattern match for safety
match (user, posts)
(Some(u), Some(p)) =>
Response { status: 200, body: render(u, p) }
(None, _) =>
Response { status: 404, body: "User not found" }
(_, None) =>
Response { status: 500, body: "Could not fetch posts" }
end
end
async function fetch_user(id: Int) -> Option<User>
# Simulate async fetch
await sleep(50)
Some(User { id: id, name: "Alice" })
end
async function fetch_posts(id: Int) -> Option<Array<Post>>
# Simulate async fetch
await sleep(50)
Some([Post { title: "Hello" }])
end
function render(user: User, posts: Array<Post>) -> String
"User: " ++ user.name ++ ", Posts: " ++ Int.toString(Array.length(posts))
end
main
let server = Server.new(port: 3000)
server.on_request(handle_request)
println("Server listening on port 3000")
println("Stability:", stability()) # Should be 100!
server.listen().await
end
# ✅ STABILITY: 100/100
#
# Why this is stable:
# ✓ Pure functions (no side effects)
# ✓ Explicit types (no type instability)
# ✓ Pattern matching (all cases handled)
# ✓ Parallel async (efficient, no blocking)
# ✓ No global state
# ✓ No mutation
#
# 🎓 LEARNING OUTCOME:
# This is what "good code" looks like in Error-Lang.
# Every decision prioritizes stability.
#
# Compare this to examples/05-web-server-unstable.err
# to see the difference!
#
# 🏆 ACHIEVEMENT UNLOCKED:
# "Async Enlightenment" - wrote async code with 100 stability