-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ox
More file actions
79 lines (60 loc) · 1.59 KB
/
main.ox
File metadata and controls
79 lines (60 loc) · 1.59 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
# Heap<T> - heap allocation with ownership
# No garbage collector - deterministic cleanup
struct LargeData
id: i32
name: str
endstruct
func demo_basic_heap()
print "=== Basic Heap Allocation ==="
# Allocate on heap
boxed: Heap<i32> = heap(42)
print "Heap value:", *boxed
# Automatic cleanup when scope ends
endfunc
func demo_heap_struct()
print ""
print "=== Heap with Structs ==="
# Large data benefits from heap
data: Heap<LargeData> = heap(LargeData {
id: 1,
name: "Large Dataset"
})
print "Data ID:", (*data).id
print "Data name:", (*data).name
endfunc
# Ownership transfer with heap values
func create_data() -> Heap<str>
return heap("Created in function")
endfunc
func process_data(data: borrow Heap<str>)
print "Processing:", **data
endfunc
func demo_heap_ownership()
print ""
print "=== Heap Ownership ==="
# Create heap data
result = create_data()
print "Received:", *result
# Borrow heap value (doesn't transfer ownership)
process_data(borrow result)
# Still own it
print "Still have:", *result
endfunc
func demo_ownership_transfer()
print ""
print "=== Ownership Transfer ==="
original: Heap<str> = heap("Hello, Heap!")
print "Original:", *original
# Transfer ownership
new_owner = give(original)
print "New owner:", *new_owner
# original is now invalid
# print *original # Error: use of moved value
endfunc
# Run demos
demo_basic_heap()
demo_heap_struct()
demo_heap_ownership()
demo_ownership_transfer()
print ""
print "=== Demo Complete ==="