2

In proposal paper for the Swift language's actor model here, it's not allowed to call function returning non-Sendable type across actors.

However, I tried the example in the paper and it can compile and run while it suppose to emit errors.

class Person: CustomStringConvertible {
  var name: String = ""

  var description: String {
    return "Person name: \(name)"
  }
}

actor BankAccount {
  var owners: [Person] = [Person()]

  func primaryOwner() -> Person? {
    return owners.first
  }
}

@main
struct EntryPoint {
  static func main() async {
    let account = BankAccount()
    if let owner = await account.primaryOwner() {
      owner.name = "FooBar" // Change actor's mutable state outside of actor-isolated context
    }
    print(await account.owners)
  }
}

// Output: [Person name: FooBar]

My Swift version

$ swift --version
Swift version 5.5.1 (swift-5.5.1-RELEASE)
Target: x86_64-unknown-linux-gnu

Did I do something wrong in the code above? If I did it right, why the implementation does not conform with the proposal paper?

Thanks.

George
  • 25,988
  • 10
  • 79
  • 133
mibu
  • 1,303
  • 11
  • 14
  • Spendable checking is not implemented yet. It's a _proposal_. – matt Nov 26 '21 at 12:20
  • @matt: I read the acceptance note but did not find anything about Sendable checking. Do you know why it has not been implemented? Has it been implemented on MacOs platform? Link to acceptance note here: https://forums.swift.org/t/accepted-with-modification-se-0306-actors/47662 – mibu Nov 26 '21 at 12:27
  • 1
    It will be implemented in Swift 6. – matt Nov 26 '21 at 12:32
  • @matt:Thank you. I've just read your question. Clearly, my question is duplicated. – mibu Nov 26 '21 at 12:44
  • 1
    Note the links in the answer (which comes from an Apple employee). These contain implementation timetable information. You can turn on Sendable checking now to get warnings. It's an interesting exercise! You will discover that a lot of your code is actually illegal. :) – matt Nov 26 '21 at 12:56
  • 1
    See also https://bugs.swift.org/browse/SR-15098 – matt Nov 26 '21 at 12:57

0 Answers0