Compiler wants to correct my closure signature to be generic?!


(Daryle Walker) #1

Going with a generalization of return types as I described here:

extension Sequence {

    func allMatches<Pattern: Collection, Result: RangeReplaceableCollection>(for pattern: Pattern, allowingOverlap: Bool, storingAs: Result.Type, by areEquivalent: (Element, Pattern.Element) throws -> Bool) rethrows -> Result where Result.Element: RangeReplaceableCollection, Result.Element.Element == Element {
        /*...*/
    }

    func allMatches<Pattern: Collection>(for pattern: Pattern, allowingOverlap: Bool, by areEquivalent: (Element, Pattern.Element) throws -> Bool) rethrows -> [[Element]] {
        return try allMatches(for: pattern, allowingOverlap: allowingOverlap, storingAs: Array<Array<Element>>.Type, by: areEquivalent)
    }

}

gives me an error while the playground checks:

error: Search.playground:365:122: error: cannot convert value of type '(Self.Element, Pattern.Element) throws -> Bool' to expected argument type '(_, _) throws -> Bool'
        return try allMatches(for: pattern, allowingOverlap: allowingOverlap, storingAs: Array<Array<Element>>.Type, by: areEquivalent)
                                                                                                                         ^~~~~~~~~~~~~
                                                                                                                                       as! (_, _) throws -> Bool

Am I doing it wrong? Or did I trigger a bug trying out rarely used functionality?


#2

This should be

Array<Array<Element>>.self

Or equivalently,

[[Element]].self

(Daryle Walker) #3

Thanks for pointing out my brain-fart.