Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15581

Multiple conformance to same protocol with different associated types

$
0
0

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?


Viewing all articles
Browse latest Browse all 15581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>