[swift-users] Strange type error

zhigang zhigang1992 at gmail.com
Fri Mar 25 01:56:04 CDT 2016


You can not mutate a array itself in a

`var asScriptable`

you might be able to accomplish such with a function.


struct AllSubscriptable<E, Index> {
    let setSubscriptee: (index: Index, element: E) -> Void
}
extension Array {
    mutating func asScriptable() -> AllSubscriptable<Element, Int> {
        func sub(key:Int, element: Element) {
                self[key] = element
        }
        return AllSubscriptable<Element, Int>(
            setSubscriptee: sub
        )
    }
}


Kyle

On Fri, Mar 25, 2016 at 2:50 PM zhigang <zhigang1992 at gmail.com> wrote:

> It might have something to do with
>
> `setSubscriptee`
>
> with seems from the implementation is a `mutating` operation.
>
> If you comment it out, or just give it a empty callback, it will pass
> compile.
>
>
> Kyle
>
> On Tue, Mar 22, 2016 at 6:23 AM Howard Lovatt via swift-users <
> swift-users at swift.org> wrote:
>
>> Hi,
>>
>> I don't understand your comment, can you explain some more please?
>>
>> Thanks,
>>
>>  -- Howard.
>>
>>
>> On Monday, 21 March 2016, zh ao <owenzx at gmail.com> wrote:
>>
>>> You protocol is not defined properly.
>>>
>>> On Mon, Mar 21, 2016 at 7:08 AM, Howard Lovatt via swift-users <
>>> swift-users at swift.org> wrote:
>>>
>>>> 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.
>>>>
>>>> _______________________________________________
>>>> swift-users mailing list
>>>> swift-users at swift.org
>>>> https://lists.swift.org/mailman/listinfo/swift-users
>>>>
>>>>
>>>
>>
>> --
>> -- Howard.
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160325/2ec2e923/attachment.html>


More information about the swift-users mailing list