// Advanced weak reference auto-nil demonstration

box Parent {
    init { name, child }
    
    pack(parentName) {
        me.name = parentName
        me.child = new Child()
        me.child.setParent(me)  // This creates 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() {
        // Check the weak reference
        return "Parent reference state checked"
    }
    
    // Test method to manually check the weak field value
    checkWeakParent() {
        return me.parent
    }
}

static box Main {
    main() {
        print("=== Weak Reference Auto-Nil Test ===")
        
        local parent = new Parent("TestParent")
        local child = parent.getChild()
        
        print("Step 1: Initial setup complete")
        print("Initial check: " + child.getParentInfo())
        
        // Check initial weak reference value
        local parentRef = child.checkWeakParent()
        print("Initial parent ref exists: yes")
        
        print("Step 2: Simulating parent 'drop' by setting to 0")
        parent = 0  // This should trigger weak reference invalidation
        
        print("Step 3: Checking weak reference after parent drop")
        local parentRef2 = child.checkWeakParent()
        print("After drop parent ref exists: still checking")
        
        return "weak reference lifecycle test completed"
    }
}