I'd like to know how this type of relation (example in kotlin) would be expressed in Swift
interface Index<K, V> {
fun getAll(key: K): Sequence<V>
}
I tried to use protocols with associated types, like this:
protocol Index {
associatedtype Key
associatedtype Value
associatedtype Result: Sequence where Sequence.Element == Value
func getAll(key: Key) -> Result
}
but this didn't work (Associated type 'Element' can only be used with a concrete type or generic parameter base
)
Then, as a workaround, i tried this:
protocol Index {
associatedtype Key
associatedtype Value
func get<T: Sequence>(key: Key) -> T where T.Element == Value
}
But this doesn't really seem like the right / idiomatic way to do it.
There are only two constraints:
- Sequence can't be a concrete type
- None of the methods on Index have a meaningful implementation
Notes:
- There will be a class / type that implements
Sequence
that is specific to each implementation ofIndex
I'm open to any other structural change! Thanks in advance.