ARC and Weak References
Lesson 29 of 48 in Coddy's Swift for Luau Developers course.
Luau uses garbage collection, and a reachable closure can keep its captured table reachable. Clearing owner does not clear the separate panel binding captured by read, so both calls still see ready. A weak-table design has different ownership, and collection timing should not be treated as ordinary scope-exit timing.
Swift ARC counts strong class ownership. The target closure deliberately captures owner weakly and returns gone after the strong owner is cleared. Strong cycles can keep instances alive; weak references do not retain and become nil after deallocation. Use unowned only when the lifetime relationship guarantees a valid object at access time.
Luau
Complete Luau console program:
main.luau
local function reader(panel: {title: string})
return function() return panel.title end
end
local owner: {title: string}? = {title = "ready"}
local read = reader(owner)
print(read())
owner = nil
print(read())
Swift
Complete program; compile the listed files together:
Panel.swift
import Foundation
final class Panel { var title = "ready" }main.swift
import Foundation
var owner: Panel? = Panel()
let read = { [weak owner] in owner?.title ?? "gone" }
print(read())
owner = nil
print(read())Challenge
EasyImplement Member with immutable name: String and init(name:). Implement Membership with weak optional member: Member? and init(member:). The membership must not keep the member alive. Input is one ASCII name of 1 through 20 letters. Create a member in an optional owner, create a membership, print its member name, clear the sole owner, and report whether the membership is now nil as name true.
Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.
Your code goes in Member.swift, Membership.swift, Functions.swift. main.swift holds the supplied input/output code and cannot be edited.
Try it yourself
import Foundation
func apiChecks(_ input: String) -> Bool {
var owner: Member? = Member(name: "Heldout"); let link = Membership(member: owner!); guard link.member === owner else { return false }; owner = nil; return link.member == nil
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Swift for Luau Developers
1Starting Swift
Working With FilesBindings and MutationNumbers and ConversionBoolean ConditionsRecap: Fare Quote4Functions and Arrays
Functions and LabelsInout and Exclusive AccessTuples and Multiple ResultsArrays Have Value SemanticsRecap: Rising Steps2Text and Decisions
Strings and InterpolationOptionals and BindingGuard and Early ExitIf and Branch ValuesRecap: Entry Rules3Control Flow
Switch and PatternsEnums as Finite ChoicesFor Loops and RangesWhile and Repeat LoopsRecap: Sequence Scan6Values and References
Structs and Independent CopiesStored and Computed PropertiesClasses and Shared IdentityARC and Weak ReferencesRecap: Value BasketPractice on your own: Swift playground