Structs conforming associated protocol storing problem

Hello! There is the following situation:

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public protocol OperationCondition {
  associatedtype Operation: ProducerOperationProtocol
  associatedtype Failure: Error
  
  static var isMutuallyExclusive: Bool { get }
  func dependency<O>(for operation: Operation) -> O? where O: ProducerOperationProtocol
  func evaluate(for operation: Operation, completion: @escaping (Result<Void, Failure>) -> Void)
}

Some class in which to store the Conditions:

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
open class ProducerOperation<Output, Failure>: Operation, ProducerOperationProtocol where Failure: Error {

   open private(set) var conditions = [AnyCondition<Self, Error>]() // Covariant problem...
   open func add<C>(_ condition: C) where C: OperationCondition { /* ... */ }
}

And here I can't do without inheritance, because too much code will have to be copied:

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
open class ConsumerProducerOperation<Input, Output, Failure>: ProducerOperation<Output, Failure> where Failure: Error {


 // open override var conditions: [AnyCondition<Self, Error>] // Covariant problem...
}

The problem is that I don't know how to store this Conditions. And I need to keep them, because they are called a little later...

The only way to store multiple instances conforming to Protocol with Associated Type (PAT), with different (or unknown) concrete type, is to use type erase.

If Operation and Failure are different/unknown you’ll need to type-erase that too.

Terms of Service

Privacy Policy

Cookie Policy