Unified Swift API: use of undeclared type T

493 Views Asked by At

I am following John Sundell's post (https://www.swiftbysundell.com/posts/providing-a-unified-swift-error-api) on using a nice perform function to create a unified error API

func perform(_ expression: @autoclosure () throws -> T,
                orThrow error: Error) throws -> T {
    do {
        return try expression()
    } catch {
        throw error
    }
}

func someFunction(url: URL) throws -> Data {
    ...
    return try perform(Data(contentsOf: url), 
                       orThrow: SearchError.dataLoadingFailed(url))
}

But it gives me Use of undeclared type error at T. Isn't Xcode supposed to figure out type T by looking at the return type of the function passed in?

1

There are 1 best solutions below

2
On BEST ANSWER

You need to declare any type parameters used in your function signature:

func perform<T>(
    _ expression: @autoclosure () throws -> T,
    orThrow error: Error
) throws -> T { ...