为什么不能在 Swift 3.2/4 KeyPath 类型别名中使用通用的 Self 引用?
示例:除非取消注释标记 // B
的行,否则以下代码将编译。 。为什么?据我所知,该行标记为 // B
应该与标记 // A
的行含义完全相同。 ,由于声明 typealias Path<T> = KeyPath<Self,T>
.
我在这里遗漏了什么,还是这是一个 Swift 编译器错误?
protocol Fooer {
associatedtype T
typealias Path<T> = KeyPath<Self, T>
var baz: T { get }
}
protocol FooPathContainable {
associatedtype F: Fooer
associatedtype T where F.T == T
var fooerPathA: KeyPath<F,T> { get set } // A
var fooerPathB: F.Path<T> { get set } // B <— causes compiler crash with Segmentation Fault 11
}
最佳答案
root,keypath 的值,在尖括号内,是声明 keypath 所必需的。这就像在 Swift 中用类型声明数组一样。 这应该有效:
var fooerPathB: F.Path<F, T> { get set }
关于swift - 为什么不能在 Swift 3.2/4 KeyPath 类型别名中使用通用的 Self 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47039328/