swift - 为什么不能在 Swift 3.2/4 KeyPath 类型别名中使用通用的 Self 引用?

标签 swift generics protocols swift4 swift3.2

为什么不能在 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/

相关文章:

Swift:如何使用关联的应用程序打开文件?

swift - 在 Swift 中,为什么子类方法不能覆盖父类(super class)中协议(protocol)扩展提供的方法

actionscript-3 - 在 AS3/Flash 中,URL 请求/加载程序可以在不指定 http 或 https 协议(protocol)的情况下使用 "//"吗?

swift - Swift 中的闭区间帮助

ios - 播放完 Swift 后关闭视频

java - 什么是原始类型,为什么我们不应该使用它呢?

c# - 为什么不能将泛型约束强制转换为其派生类型?

ios - 重新分配SKScene委托(delegate)协议(protocol)

ios - 更改 spritekit 中的初始场景

java - 使用泛型进行反序列化的重构方法