Menu
CoddyTech

Access Control and Invariants

Lesson 39 of 48 in Coddy's Swift for Luau Developers course.

Luau can hide state in a lexical local captured by public functions. Opened is not a writable table field in this latch, although callers could replace the public function entries. A record type or underscore prefix alone would not create compiler-enforced private instance storage.

Swift private(set) exposes reads at the getter's access level while restricting mutation. The latch method checks its invariant before changing state, so the second call rejects the transition. Private, fileprivate and internal have declaration, file and module boundaries rather than Luau-style table naming conventions.

Luau

Complete Luau console program:

main.luau

local function make_latch()
  local opened = false
  return {
    opened = function() return opened end,
    open = function()
      if opened then return false end
      opened = true
      return true
    end
  }
end
local latch = make_latch()
print(tostring(latch.open()) .. " " .. tostring(latch.open()) .. " " .. tostring(latch.opened()))

Swift

Complete program; compile the listed files together:

Latch.swift

import Foundation
final class Latch {
    private(set) var opened = false
    func open() -> Bool {
        guard !opened else { return false }
        opened = true
        return true
    }
}

main.swift

import Foundation
let latch = Latch()
print(latch.open(), latch.open(), latch.opened)
challenge icon

Challenge

Easy

Implement Wallet(initial:) with read-only-to-callers balance, and spend(_ amount: Int) -> Bool. Reject nonpositive or unaffordable spends without changing balance; otherwise subtract and return true. Initial balance is nonnegative. Input is initial balance 0 through 1000 followed by 0 through 30 spends -10 through 1000. Output final balance then one space then number of successful spends.

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 Wallet.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 {
let w = Wallet(initial: 17); guard !w.spend(18) && !w.spend(0) && !w.spend(-1) && w.balance == 17 else { return false }; return w.spend(7) && w.balance == 10 && w.spend(10) && w.balance == 0 && !w.spend(1)
}
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") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for Luau Developers

Practice on your own: Swift playground