Trying to create a common data source, I came across this error, and I wondered why this could not be compiled.
Mistake:
Cannot convert return expression of type '[Int]' to return type '[Int]'
Code:
protocol DataSource {
func getData<T> () -> [T]
}
class IntDataSource<Int>: DataSource {
let data:[Int] = []
func getData<Int>() -> [Int] {
return data
}
}
The error is displayed on the return statement in IntDataSource.
I know that this could be done better with
typealias DataType
var data: DataType? { get }
But I'm mostly interested in why the compiler doesn't want to accept the return statement. Any ideas?
EDIT:
Partially, the question is why, if the previous code does not compile, is this the next fair game?
class IntDataSource<Int>: DataSource {
func getData<Int>() -> [Int] {
let data:[Int] = []
return data
}
}
EDIT 2:
This version also compiles without problems.
class IntDataSource<Int>: DataSource {
func getData<Int>() -> [Int] {
return getIntData()
}
func getIntData<Int>() -> [Int] {
let data:[Int] = []
return data
}
}
source
share