Roman
Roman

Reputation: 1498

A problem with new "count(where:)" syntax in swift 5

The problem is it doesn't compile.
Looks like there is no such a syntax.
But the corresponding proposal is marked as accepted.
And they use the syntax in several trusted articles: for example here and in this video.
The exact code from video above doesn't compile:

import Foundation

let scores = [100, 80, 85]
let passCount = scores.count { $0 >= 85 }

Error:

Cannot call value of non-function type 'Int'

Upvotes: 3

Views: 159

Answers (1)

Martin R
Martin R

Reputation: 539775

SE-0220 has been accepted, but not yet implemented in Swift 5.

In fact an initial implementation had to be reverted due to problems with the type checker. For more information, see Require parameter names when referencing to functions in the Swift forum:

Some of you may remember SE-0220, my proposal that added the count(where:) function to sequences. This proposal was ultimately accepted in time for Swift 5, but sadly had to be reverted because it was causing issues with the type checker.

The issue was that when you reference count, in an expression like myArray.count * 5, you could be referring to the count property, with type Int, or the count(where:) function, which has type ((Element) -> Bool) -> Int, which you can refer to in shorthand as count. When Swift tries to resolve what version of the * function to use, it has to run through a lot more potential implementations, which explode combinatorially as you increase the number of operators in the expression.

Upvotes: 5

Related Questions