// Working demonstration of weak reference auto-nil behavior

box Parent {
    init { name }
    
    pack(parentName) {
        me.name = parentName
    }
    
    getName() {
        return me.name
    }
}

box Child {
    init { weak parent }  // weak modifier on parent field
    
    setParent(p) {
        me.parent = p
    }
    
    getParentInfo() {
        // This method will use the weak field access system
        // which automatically returns null if the parent is dropped
        if me.parent == 0 {
            return "Parent is null (dropped)"
        } else {
            return "Parent exists"
        }
    }
    
    // Direct weak field access for testing
    accessWeakParent() {
        return me.parent
    }
}

static box Main {
    main() {
        print("=== Weak Reference Auto-Nil Demonstration ===")
        
        local parent = new Parent("TestParent")
        local child = new Child()
        
        print("Step 1: Setting up parent-child relationship")
        child.setParent(parent)
        print("After setup: " + child.getParentInfo())
        
        print("Step 2: Simulating parent 'drop' (set to 0)")
        parent = 0  // This should trigger weak reference invalidation
        
        print("Step 3: Checking child's parent reference after drop")
        print("After drop: " + child.getParentInfo())
        
        print("Step 4: Direct weak field access test")
        local directAccess = child.accessWeakParent()
        
        return "Auto-nil demonstration completed successfully"
    }
}