Search code examples
swiftgenericsswift3

Swift: error: use of undeclared type 'T'


Swift 3.0 and getting this error, unsure why:

Code:

func rest(_ list: ArraySlice<T>) -> ArraySlice<T> {
    return list.dropFirst()
}

Error:

error: repl.swift:1:48: error: use of undeclared type 'T'
func rest(_ list: ArraySlice<T>) -> ArraySlice<T> {
                                               ^

Solution

  • You need to specify the generic parameter of ArraySlice, just using as ArraySlice<T> does not declare T:

    func rest<T>(_ list: ArraySlice<T>) -> ArraySlice<T> {
        return list.dropFirst()
    }
    

    Or:

    class MyClass<T> {
        func rest(_ list: ArraySlice<T>) -> ArraySlice<T> {
            return list.dropFirst()
        }
    }