how do i disambiguate a call to a generic function with a function parameter in swift?

The issue here is not the generic parameters. Your first and second attempt would tell the compiler what type T should be.

The issue is the value you pass as callback, which has the following signature:

(Parser) -> () throws -> T

You are passing in self.parseType which has the following signature:

() throws -> String

What would work is using Self.parseType (notice the capital S) or Parser.parseType as value for callback.

Alternatively, you could define maybe like this:

public func maybe<T>(_ callback: (Parser) throws -> T) -> T? {
    do {
        return try callback(self)
    } catch {
        return nil
    }
}

And then call it like this:

let type = self.maybe { try $0.parseType() }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top