0

I would like to have an array of SomeProtocol which has a get property represents in Task extension.

If hashValue of Task is unique, I would like to consider it as a primary key to remove object in SomeProtocol array. And I wonder it is guaranteed on memory level, too.

Here's code below.

protocol SomeProtocol {
    var taskId: Int { get }
}


extension Task: SomeProtocol {
    var taskId: Int {
        get { return hashValue }
    }
}
dev Crying
  • 55
  • 1
  • 7

1 Answers1

3

By definition, a hash value is not unique. If you want to assign a unique ID to your tasks, you will probably need to handle it yourself:

struct IdentifiableTask<E,F>: Identifiable where F:Error {
    let id = UUID()
    let task: Task<E,F>
    
    init(_ task: Task<E,F>) {
        self.task = task
    }
}
Scott Thompson
  • 22,629
  • 4
  • 32
  • 34