Return 一种不透明类型的数组

Return an opaque type of array

可以return some [T]吗?

protocol P {
    associatedtype X
    func method() -> [X]
}

class Imp: P {
    typealias X = Int

    func method() -> some [Int] {
        return [1]
    }
}

上面的代码产生错误 "An 'opaque' type must specify only 'Any', 'AnyObject', protocols, and/or a base class"

编辑: 所以协议隐藏了底层 @NSMangedObject 并只公开了需要的属性。如果A、B有Comparable能力就好了

这是不可能的,但那是因为它没有任何意义。 some T 表示 "a specific, concrete type that conforms to T, known by the returning function at compile time, but not known by the caller." [Int] 是调用者已知的类型。没有什么 "opaque" 。这等同于:

func method() -> [Int] { ... }