无法将 associatedType 用作 swift 中的方法 returnType,因为编译器会抛出 "Ambiguous inference of associated type"
Unable to use associatedType as method returnType in swift as compiler throws "Ambiguous inference of associated type"
正在玩 associatedType。使用 associatedType 时,编译器无法识别方法的 return 类型。
这是代码示例,
protocol DummyOffice {
}
struct EmptyOffice: DummyOffice {
}
protocol Office {
associatedtype SubBranch: DummyOffice
var subBranch: SubBranch { get }
func getSubBranch() -> SubBranch
}
struct Apple: Office {
let emptyOffice = EmptyOffice()
func getSubBranch() -> some DummyOffice {
return EmptyOffice()
}
var subBranch: some DummyOffice {
return EmptyOffice()
}
}
编译器抛出这个错误。
问题:
(1) 属性“subBranch”没有发生错误。那就是如果我没有在协议中创建 returns associatedType 的方法,一切都会顺利进行。
正如@zaitsman 所建议的,将 associatedTypes 替换为 typealias 效果很好。
But typealias cannot be used as psuedo generic constraint,
protocol DummyOffice {
}
struct EmptyOffice: DummyOffice {
}
protocol Office {
typealias SubBranch = DummyOffice
var subBranch: SubBranch { get }
func getSubBranch() -> SubBranch
}
struct Apple: Office {
var subBranch: Self.SubBranch
func getSubBranch() -> Self.SubBranch {
return EmptyOffice()
}
}
您不需要使用不透明的 return 类型(即 some
)。当你去实现 Office
协议时,只需 return 来自函数的 actual 类型和你指定的计算 属性 ,编译器将推断associatedtype
给你:
protocol DummyOffice {}
struct EmptyOffice: DummyOffice {}
protocol Office {
associatedtype SubBranch: DummyOffice
var subBranch: SubBranch { get }
func getSubBranch() -> SubBranch
}
struct Apple: Office {
let emptyOffice = EmptyOffice()
func getSubBranch() -> EmptyOffice {
return EmptyOffice()
}
var subBranch: EmptyOffice {
return EmptyOffice()
}
}
正在玩 associatedType。使用 associatedType 时,编译器无法识别方法的 return 类型。
这是代码示例,
protocol DummyOffice {
}
struct EmptyOffice: DummyOffice {
}
protocol Office {
associatedtype SubBranch: DummyOffice
var subBranch: SubBranch { get }
func getSubBranch() -> SubBranch
}
struct Apple: Office {
let emptyOffice = EmptyOffice()
func getSubBranch() -> some DummyOffice {
return EmptyOffice()
}
var subBranch: some DummyOffice {
return EmptyOffice()
}
}
编译器抛出这个错误。
问题:
(1) 属性“subBranch”没有发生错误。那就是如果我没有在协议中创建 returns associatedType 的方法,一切都会顺利进行。
正如@zaitsman 所建议的,将 associatedTypes 替换为 typealias 效果很好。
But typealias cannot be used as psuedo generic constraint,
protocol DummyOffice {
}
struct EmptyOffice: DummyOffice {
}
protocol Office {
typealias SubBranch = DummyOffice
var subBranch: SubBranch { get }
func getSubBranch() -> SubBranch
}
struct Apple: Office {
var subBranch: Self.SubBranch
func getSubBranch() -> Self.SubBranch {
return EmptyOffice()
}
}
您不需要使用不透明的 return 类型(即 some
)。当你去实现 Office
协议时,只需 return 来自函数的 actual 类型和你指定的计算 属性 ,编译器将推断associatedtype
给你:
protocol DummyOffice {}
struct EmptyOffice: DummyOffice {}
protocol Office {
associatedtype SubBranch: DummyOffice
var subBranch: SubBranch { get }
func getSubBranch() -> SubBranch
}
struct Apple: Office {
let emptyOffice = EmptyOffice()
func getSubBranch() -> EmptyOffice {
return EmptyOffice()
}
var subBranch: EmptyOffice {
return EmptyOffice()
}
}