符合协议时类型别名声明的冗余重复(第 2 部分)
Redundant duplication of typealiase declarations when conforms to a protocol (Part 2)
protocol Destinationable: Hashable {
associatedtype D: Hashable
var destination: D { get }
}
protocol Graph {
associatedtype Edge: Destinationable
subscript(node: D) -> Set<Edge>! { get set }
}
extension Graph {
typealias D = Edge.D
}
struct UndirectedGraph<Edge: Destinationable>: Graph {
typealias D = Edge.D // Why should we again declare this typealias!?
private var storage: [D: Set<Edge>]
subscript(node: D) -> Set<Edge>! {
get { storage[node] }
set { storage[node] = newValue }
}
}
这行得通。但是如果我在结构中删除 typealias D = Edge.D
的重新声明,我会得到一个与下标相关的编译错误:
Unsupported recursion for reference to type alias 'D' of type
'UndirectedGraph'
为什么会这样?什么递归???
我不确定什么是递归,但你应该避免在 protocol extensions 中声明类型别名,而是使用相同类型的约束:
protocol Destinationable: Hashable {
associatedtype D: Hashable
var destination: D { get }
}
protocol Graph {
associatedtype D
associatedtype Edge: Destinationable where D == Edge.D
subscript(node: D) -> Set<Edge>! { get set }
}
struct UndirectedGraph<Edge: Destinationable>: Graph {
private var storage: [D: Set<Edge>]
subscript(node: Edge.D) -> Set<Edge>! {
get { storage[node] }
set { storage[node] = newValue }
}
}
编译得很好,但在你的情况下:
protocol Destinationable: Hashable {
associatedtype D: Hashable
var destination: D { get }
}
protocol Graph {
associatedtype Edge: Destinationable
subscript(node: Edge.D) -> Set<Edge>! { get set }
}
struct UndirectedGraph<Edge: Destinationable>: Graph {
private var storage: [Edge.D: Set<Edge>]
subscript(node: Edge.D) -> Set<Edge>! {
get { storage[node] }
set { storage[node] = newValue }
}
}
可能就够了。
protocol Destinationable: Hashable {
associatedtype D: Hashable
var destination: D { get }
}
protocol Graph {
associatedtype Edge: Destinationable
subscript(node: D) -> Set<Edge>! { get set }
}
extension Graph {
typealias D = Edge.D
}
struct UndirectedGraph<Edge: Destinationable>: Graph {
typealias D = Edge.D // Why should we again declare this typealias!?
private var storage: [D: Set<Edge>]
subscript(node: D) -> Set<Edge>! {
get { storage[node] }
set { storage[node] = newValue }
}
}
这行得通。但是如果我在结构中删除 typealias D = Edge.D
的重新声明,我会得到一个与下标相关的编译错误:
Unsupported recursion for reference to type alias 'D' of type 'UndirectedGraph'
为什么会这样?什么递归???
我不确定什么是递归,但你应该避免在 protocol extensions 中声明类型别名,而是使用相同类型的约束:
protocol Destinationable: Hashable {
associatedtype D: Hashable
var destination: D { get }
}
protocol Graph {
associatedtype D
associatedtype Edge: Destinationable where D == Edge.D
subscript(node: D) -> Set<Edge>! { get set }
}
struct UndirectedGraph<Edge: Destinationable>: Graph {
private var storage: [D: Set<Edge>]
subscript(node: Edge.D) -> Set<Edge>! {
get { storage[node] }
set { storage[node] = newValue }
}
}
编译得很好,但在你的情况下:
protocol Destinationable: Hashable {
associatedtype D: Hashable
var destination: D { get }
}
protocol Graph {
associatedtype Edge: Destinationable
subscript(node: Edge.D) -> Set<Edge>! { get set }
}
struct UndirectedGraph<Edge: Destinationable>: Graph {
private var storage: [Edge.D: Set<Edge>]
subscript(node: Edge.D) -> Set<Edge>! {
get { storage[node] }
set { storage[node] = newValue }
}
}
可能就够了。