Cannot convert [Int] to [Int] in a common implementation

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
    }
}
+4
source share
1 answer

""

Cannot convert return expression of type '[Int]' to return type '[Int]'

:

<Int> <Int> Int (, , <Int> , <Int> ).

class IntDataSource<A>: DataSource {
    let data:[A] = []
    func getData<B>() -> [B] {
        return data 
    }
}

:

cannot convert return expression of type '[A]' to return type '[B]'
+6

Source: https://habr.com/ru/post/1617219/


All Articles