Swift subprotocol with associated type
Asked Answered
O

1

5

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:

  1. Sequence can't be a concrete type
  2. 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 of Index

I'm open to any other structural change! Thanks in advance.

Oersted answered 23/2, 2018 at 23:8 Comment(0)
C
16

You need to use the associated type's name, not the inherited protocol's name:

associatedtype Result: Sequence where Result.Element == Value
//                                    ^^^^^^

Note that Swift's standard library now includes a type named Result, so you might want to use a different name for your associatedtype. I use Answer in my own code:

associatedtype Answer: Sequence where Answer.Element == Value

(In Smalltalk, return values are called “answers”.)

Codeine answered 23/2, 2018 at 23:13 Comment(3)
That's really interesting. And it makes sense, of course. It would be somewhat wise for the Swift team to make a guide for non objective-c developers as the syntax is pretty different from other languages.Oersted
Does this still work? I am getting "'A' is not a member type of 'Self.Result'".Blinders
Actually it never worked. My example involving Either had several flaws so I've removed it.Codeine

© 2022 - 2024 — McMap. All rights reserved.