[Request]: Introduce type-safe identifier to prevent accidentally mixing up different resource ID types at compile time.
- Dominant language
- Swift
- Stars
- 49.9k
- Forks
- 1.8k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 22
Description
### Feature or enhancement request details
### Context
Discussed in PR #1175 ([r2778450020](https://github.com/apple/container/pull/1175#discussion_r2778433459)), currently IDs are plain `String`, which is not ideal IMO and maybe prone to accidental misuse sometimes.
### Suggestion
Introduce a type-safe identifier to replace plain `String` and other types. Something akin to;
```Swift
public protocol Identifiable {
associatedtype RawIdentifier: Hashable & Codable & Sendable = String
typealias ID = Identifier
var id: ID { get }
}
public struct Identifier: Hashable, Codable, Sendable {
public let rawValue: Value.RawIdentifier
public init(rawValue: Value.RawIdentifier) { self.rawValue = rawValue }
}
```
#### Usage
```Swift
// String type
struct AContainer: Identifiable {
let id: ID
}
// Int type
struct AProcess: Identifiable {
typealias RawIdentifier = Int
let id: ID
}
// UUID type
struct AVolume: Identifiable {
typealias RawIdentifier = UUID
let id: ID
}
// So instead of
public struct AContainerList {
public var ids: [String]
}
// It will be
public struct AContainerList {
public var ids: [AContainer.ID]
}
```
Let me know if this worth of a small addition.
### Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Contributor guide
Research direction
Start by reading the discussion in PR #1175, which motivated replacing plain String resource IDs. Then map the current ID declarations and usages across the project; done means the scope and identifier design are agreed and the affected resource IDs can no longer be accidentally mixed at compile time.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100