The earliest discussion of this is here.
I need a generic wrapper with its own unique-id space
.
enum my {
// A wrapper with its own unique-id space
struct Wrapper <T>: Identifiable {
let value: T
let id : Int = Self.uniqueID ()
private static var uniqueID = UIDStore ()
// Error: Static stored properties not supported in generic types
}
struct UIDStore {
private var value: Int = 0
mutating func callAsFunction () -> Int {
defer {value += 1}
return value
}
}
}
Currently I find myself doing this, writing a wrapper for each specific type I need.
extension my {
// An Int wrapper with its own unique-id space
struct IntWrapper: Identifiable {
let value: Int
let id : Int = Self.uniqueID ()
private static var uniqueID = UIDStore ()
}
}
extension my {
// A String wrapper with its own unique-id space
struct StringWrapper: Identifiable {
let value: String
let id : Int = Self.uniqueID ()
private static var uniqueID = UIDStore ()
}
}
Can anyone guide me towards an elegant solution, one that still uses the generic type parameter?
Edit - PS: Want id's to be monotonically rising and be in the range 0..<N
for a given type.