rayx
(Huan Xiong)
1
Hi, I guess I must be missing something, but couldn't figure out what exactly it is. Can anyone please explain why this fails to compile?
protocol Foo: BidirectionalCollection {
var index: Index { get }
func getIndices() -> [Index]
}
extension Foo {
func getIndices() -> [Index] {
Array(index..<endIndex)
// Error: No exact matches in call to initializer
}
}
If I do the similar on a concreate type (e.g. an array), it works fine.
let x: [Int] = [1, 2, 3, 4]
let y = Array(x.startIndex..<x.endIndex)
I think the initializer in question is this. I can't see there are any reason why it doesn't work in the first case.
mayoff
(Rob Mayoff)
2
That Array initializer requires its argument to conform to Sequence.
Range conforms to Sequence conditionally:
Conforms when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger .
You need to add those constraints to your getIndices method:
extension Foo {
func getIndices() -> [Index] where Index: Strideable, Index.Stride: SignedInteger {
Array(index..<endIndex)
}
}
3 Likes
cukr
4
By default Xcode is hiding the full error message, to get the notes you have to click on the issue navigator in the left pane (FB11369761)
1 Like
rayx
(Huan Xiong)
5
Thanks all for your reply. They are valuable. I probably would never notice them (the conditional conformance of Range and the full error message in navigation pane) if you didn't say it.
1 Like