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.
source share