Is it possible to join two type parameter packs to create another type?
Let's see the issue with a sample. Given the following protocol:
protocol Node {}
I create some concrete types:
struct Heading: Node {}
struct Paragraph: Node {}
struct Quote: Node {}
struct Image: Node {}
struct Table: Node {}
I also have a simple grouping type:
struct Group<each N: Node> {
let nodes: (repeat each N)
init(_ nodes: repeat each N) {
self.nodes = (repeat each nodes)
}
}
If I create two groups:
let lhs = Group(Heading(), Paragraph(), Quote())
// Group<Pack{Heading, Paragraph, Quote}>
let rhs = Group(Image(), Table())
// Group<Pack{Image, Table}>
Is it possible to join the two groups in a new group? I am not able to create a group eventhough the compiler is inferring the types correctly:
func joinNodes<each N1: Node, each N2: Node>(
lhs: Group<repeat each N1>,
rhs: Group<repeat each N2>
) -> (repeat each N1, repeat each N2) {
(repeat each lhs.nodes, repeat each rhs.nodes)
}
let nodes = joinNodes(lhs: lhs, rhs: rhs)
// type(of: nodes): (Heading, Paragraph, Quote, Image, Table)
The compiler fails to compile the following line with error: Type '(Heading, Paragraph, Quote, Image, Table)' cannot conform to 'Node'
.
let group = Group(nodes)
As far as I understand, the compiler is telling me that the type parameter pack is not conforming to Node
, which is true. But each individual type does conform to Node
, which it is what the initializer expects.