[swift-users] Range subscript is ambiguous
Neil Faiman
neil.swift at faiman.org
Sun May 15 07:31:56 CDT 2016
This function seems simple enough:
func foo(a: [Int], n: Int) {
var x : [Int] = a[0..<n]
}
But it doesn’t compile.
error: ambiguous subscript with base type '[Int]' and index type 'Range<Int>'
var x : [Int] = a[0..<n]
~^~~~~~~
Swift.Array:100:12: note: found this candidate
public subscript (subRange: Range<Int>) -> ArraySlice<Element> { get set }
^
Swift.MutableCollectionType:3:12: note: found this candidate
public subscript (bounds: Range<Self.Index>) -> MutableSlice<Self> { get set }
^
Swift.CollectionType:2:12: note: found this candidate
public subscript (bounds: Range<Self.Index>) -> Slice<Self> { get }
^
The oddity is that if I change the assignment to this
var y : [Int] = Array(a[0..<n])
then the compiler is happy.
Shouldn’t it be able to do any necessary type inference from the fact that the expression is in a context where an array is required?
Neil Faiman
More information about the swift-users
mailing list