swift 5 中新 "count(where:)" 语法的问题
A problem with new "count(where:)" syntax in swift 5
问题是它无法编译。
好像没有这样的语法。
但是 the corresponding proposal 被标记为已接受。
他们在几篇可信的文章中使用了语法:例如 here and in this video.
上面视频中的确切代码无法编译:
import Foundation
let scores = [100, 80, 85]
let passCount = scores.count { [=12=] >= 85 }
错误:
Cannot call value of non-function type 'Int'
SE-0220 已被接受,但尚未在 Swift 中实施 5.
事实上,Swift 论坛中的初始实施必须是 reverted due to problems with the type checker. For more information, see Require parameter names when referencing to functions:
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.
问题是它无法编译。
好像没有这样的语法。
但是 the corresponding proposal 被标记为已接受。
他们在几篇可信的文章中使用了语法:例如 here and in this video.
上面视频中的确切代码无法编译:
import Foundation
let scores = [100, 80, 85]
let passCount = scores.count { [=12=] >= 85 }
错误:
Cannot call value of non-function type 'Int'
SE-0220 已被接受,但尚未在 Swift 中实施 5.
事实上,Swift 论坛中的初始实施必须是 reverted due to problems with the type checker. For more information, see Require parameter names when referencing to functions:
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 thecount
property, with typeInt
, or thecount(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.