phase: 20.49 COMPLETE; 20.50 Flow+String minimal reps; 20.51 selfhost v0/v1 minimal (Option A/B); hv1-inline binop/unop/copy; docs + run_all + CURRENT_TASK -> 21.0
This commit is contained in:
54
local_tests/test_weak_reference_complete.hako
Normal file
54
local_tests/test_weak_reference_complete.hako
Normal file
@ -0,0 +1,54 @@
|
||||
// Complete weak reference auto-nil test
|
||||
|
||||
box Parent {
|
||||
init { name, child }
|
||||
|
||||
pack(parentName) {
|
||||
me.name = parentName
|
||||
me.child = new Child()
|
||||
me.child.setParent(me) // This should create a weak reference
|
||||
}
|
||||
|
||||
getChild() {
|
||||
return me.child
|
||||
}
|
||||
|
||||
getName() {
|
||||
return me.name
|
||||
}
|
||||
}
|
||||
|
||||
box Child {
|
||||
init { weak parent } // weak modifier on parent field
|
||||
|
||||
setParent(p) {
|
||||
me.parent = p
|
||||
}
|
||||
|
||||
getParentInfo() {
|
||||
// Instead of comparing to null, check if parent field exists and is valid
|
||||
local parentExists = me.parent != 0 // Using 0 as a null check workaround
|
||||
if parentExists {
|
||||
return "Parent exists"
|
||||
} else {
|
||||
return "Parent is null (dropped)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static box Main {
|
||||
main() {
|
||||
local p = new Parent("TestParent")
|
||||
local child = p.getChild()
|
||||
|
||||
print("Initial: " + child.getParentInfo())
|
||||
|
||||
// Test the weak reference system
|
||||
// When p goes out of scope, child.parent should automatically become null
|
||||
p = 0 // Setting to 0 to simulate parent dropping
|
||||
|
||||
print("After drop: " + child.getParentInfo())
|
||||
|
||||
return "weak reference auto-nil test completed"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user