ObjC/Swift swift 在设计上是不是有点毛病啊,我也说不出

miao · September 12, 2020 · Last by itomato replied at September 15, 2020 · 1686 hits

这能看吗?

哪里奇怪

Reply to aloveric

并不是奇怪 只是觉得复杂头都绕晕了,为了设计标准库的容器协议,会写出这么复杂的 constrain,好吧这肯定是有水平的,但我如果写完类似的代码我是崩溃的,至少头脑里会报警,会想是不是走上了歧途。

Reply to miao

这是文档的锅,主要是这里有一个递归的声明 SubSequence.SubSequence == SubSequence,所以文档生成器才被绕进去了,实际声明的代码其实很简单:

// https://github.com/apple/swift/blob/master/stdlib/public/core/Collection.swift
protocol Collection: Sequence {
  override associatedtype Element
  associatedtype Index: Comparable
  associatedtype SubSequence: Collection where SubSequence.Index == Index,
        Element == SubSequence.Element,
        SubSequence.SubSequence == SubSequence
  ...
}

Swift 本身还是不错的,槽点不多,苹果的文档确实太坑爹。

You need to Sign in before reply, if you don't have an account, please Sign up first.