Why does the fast compiler sometimes fail to accept the name of the shorthand argument?

Array: var closestAnnotations:[MKAnnotation]

I was wondering why the fast compiler will not 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 }) 
+5
source share
1 answer

I tested 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 type test, works without problems.

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

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

However, this does not work:

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

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

This leads me to conclude that the expression $0.dynamicType somehow optimized.

The most interesting

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

causes the same error.

So, I think there are two compiler errors:

  • The compiler cannot deduce the argument from the type of the array and not, because (_) -> Bool should be considered as (Type) -> Bool when calling [Type] .

  • The compiler somehow optimizes $0.dynamicType out and this is clearly wrong.

+1
source

All Articles