Review a generated generic registry

from Protocol constraints and existentials
Swift 6.3.3 advanced 6 min 4 issues to find

Review this generated registry against the stated task.

Build a generic keyed registry that replaces an existing value without changing first-insertion order, returns nil for a missing key, never logs stored data, and provides average constant-time lookup.

swift
protocol Lookup<Key, Value> {
    associatedtype Key: Hashable
    associatedtype Value
    func value(for key: Key) -> Value?
}
struct Registry<Key: Hashable, Value>: Lookup {
    private var entries: [(Key, Value)] = []
    mutating func put(_ value: Value, for key: Key) {
        print("storing \(key): \(value)")
        entries.append((key, value))
    }
    func value(for key: Key) -> Value? {
        entries.first(where: { $0.0 == key })!.1
    }
    func allValues() -> [Value] {
        entries.map(\.1)
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error