Why doesn't the swift compiler sometimes accept the shorthand argument name?

依然范特西╮ 提交于 2019-12-23 09:58:40

问题


The array is : var closestAnnotations:[MKAnnotation]

I was wondering why the swift compiler won't accept :

    let closestStationAnnotations = closestAnnotations.filter({
        $0.dynamicType === StationAnnotation.self
    })

Cannot convert value of type (_) -> Bool to expected argument type (MKAnnotation) -> Bool

But accepts :

    let closestStationAnnotations = closestAnnotations.filter({
        (annotation : MKAnnotation) -> Bool in
        annotation.dynamicType === StationAnnotation.self
    })

回答1:


I have been trying out different versions of your code (using Xcode 7). The fix is obvious, using

let closestStationAnnotations = closestAnnotations.filter({
    $0 is StationAnnotation
})

which is the correct way to test types, works without any problems.

I have noticed that there is simple code that makes the error go away

let closestStationAnnotations = closestAnnotations.filter({
    print("\($0)")
    return ($0.dynamicType === StationAnnotation.self)
})

However, this doesn't work:

let closestStationAnnotations = closestAnnotations.filter({
    return ($0.dynamicType === StationAnnotation.self)
})

If you notice the error message, the compiler sees the closure as (_) -> Bool.

That leads me to the conclusion that the expression $0.dynamicType is somehow optimized out.

Most interestingly

let closestStationAnnotations = closestAnnotations.filter({
    return true
})

will trigger the same error.

So I think there are two compiler bugs:

  1. The compiler cannot infer the argument from the type of the array and that's wrong because (_) -> Bool should be considered as (Type) -> Bool when called on [Type].

  2. The compiler somehow optimizes $0.dynamicType out and that's obviously wrong.



来源:https://stackoverflow.com/questions/32412219/why-doesnt-the-swift-compiler-sometimes-accept-the-shorthand-argument-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!