[swift-users] Strange type error

Howard Lovatt howard.lovatt at gmail.com
Sun Mar 20 18:08:19 CDT 2016


HI,

Does anyone know what is happening here:

protocol Subscriptable {

    associatedtype Element

    associatedtype Index

    var firstIndex: Index { get }

    var lastIndex: Index { get }

    subscript(index: Index) -> Element { get set }

}


struct AllSubscriptable<E, I>: Subscriptable {

    typealias Element = E

    typealias Index = I

    let firstIndexee: () -> I

    let lastIndexee: () -> I

    let subscriptee: (index: I) -> E

    let setSubscriptee: (index: I, element: E) -> Void

    var firstIndex: I { return firstIndexee() }

    var lastIndex: I { return lastIndexee() }

    subscript(index: I) -> E {

        get { return subscriptee(index: index) }

        set { setSubscriptee(index: index, element: newValue) }

    }

}


extension Array {

    var asScriptable: AllSubscriptable<Element, Int> {

        return AllSubscriptable(

            firstIndexee: { return self.startIndex }, // Error: Cannot
convert `() -> Int` to expected `() -> _`

            lastIndexee: { return self.endIndex },

            subscriptee: { return self[$0] },

            setSubscriptee: { self[$0] = $1 }

        )

    }

}


The error, "Cannot convert `() -> Int` to expected `() -> _`", on the
`firstIndexee` closure is very strange since the types are correct and the
same as `lastIndexee` which does not have an error.

Also if the set subscript in Subscriptable and then all the other set
support in AnySubscriptabe and in the extension is removed, then no error.

Any clues to why?

-- Howard.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160321/a3009963/attachment.html>


More information about the swift-users mailing list