Reputation: 10485
I am watching WWDC about actors and I don't understand something. Consider the following code
class ID: Hashable {
var id: Int
init(id: Int) {
self.id = id
}
static func == (lhs: ID, rhs: ID) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
actor LibraryAccount {
let id: ID
var booksOnLoan: [Book]
init(id: ID, booksOnLoan: [Book]) {
self.id = id
self.booksOnLoan = booksOnLoan
}
}
extension LibraryAccount: Equatable {
static func == (lhs: LibraryAccount, rhs: LibraryAccount) -> Bool {
lhs.id == rhs.id
}
}
extension LibraryAccount: Hashable {
nonisolated func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
This code compiles with no warning with strict concurrency checking. The important thing here is of course that ID
is a non-final class with id
being a var
, so definitely not sendable.
Suppose I call ==
on two instances of LibraryAccount
, and they both have the same id. I read ID.id
from the first, then another thread changes the id and then the ==
returns true, at which point the ids are already different.
Am I missing something here?
Upvotes: 1
Views: 48