Good day, sir or madam!
I have a family of very similar protocols, and, naturally, would like to avoid code duplication by moving the common part to a parent like this:
public protocol EntityProcessor<T> {
associatedtype T: Entity
func insert(_ item: T) throws
func delete(_ item: T) throws
func update(_ item: T) throws
}
public protocol ExpenseService: EntityProcessor<Expense> { }
public protocol BudgetService: EntityProcessor<Budget> { }
And then use them as follows:
public class DataStoreImplementation: ExpenseService & BudgetService {
func insert(_ item: Expense) throws { }
func insert(_ item: Budget) throws { }
// ...
}
However, the compiler can’t figure it out as it can’t have multiple different values for T in a conforming type.
Is there a proper way to implement this idea with similar brevity?