0

I have a protocol like this:

protocol P {
  func doThingWithTypeArgument<T>(_ type: T.Type) -> T
}

extension P {
  func doThingWithInferredType<T>() -> T {
    self.doThingWithTypeArgument(T.self)
  }
}

When self is sendable, the extension method doThingWithInferredType is sendable as well, since it captures no external state. How do I mark this? An extension like

extension P
where Self: Sendable {
  @Sendable
  func doThingWithInferredType<T>() -> T
}

complains that the extension method has no body.

I don't believe this @Sendable annotation requires the base method, doThingWithTypeArgument, to be @Sendable, since you can call non-sendable methods of sendable types from a sendable function:

struct S: Sendable {
  func f() {}
}

let s = S()

let f1: @Sendable () -> Void = s.f // error, since f isn't @Sendable
let f2: @Sendable () -> Void = { s.f() } // ok, since s is Sendable

If it does, however, that only makes this conditional attribute more complicated (and perhaps impossible without a second protocol like SendableP).

Bbrk24
  • 739
  • 6
  • 22
  • Re the error, protocol extensions are for adding default implementations, hence it is confused about the lack of a body. – Rob Oct 28 '22 at 01:17

0 Answers0