I've been trying to reference an associated type with itself:
struct Composite<Foo> { }
protocol Chain {
associatedtype Next: Chain
associatedtype Foo = Composite<Next.Foo> // Associated type `Foo` references itself.
}
Is this too eager of a check? Since I can still technically break a chain:
enum EndChain: Chain {
typealias Next = Self
typealias Foo = Double
}
enum A: Chain {
typealias Next = EndChain
typealias Foo = Composite<Next.Foo> // Ok
}
enum B: Chain {
typealias Next = A
typealias Foo = Composite<Next.Foo> // Ok
}
...
And I want to reduce the amount of Foo
I need to type.
Most important part is that I can match the "Chain" size with the Composite
count:
"FullChain<A>".Foo = Composite<Double>
"FullChain<A, B>".Foo = Composite<Composite<Double>>
"FullChain<A, B, C>".Foo = Composite<Composite<Composite<Double>>>
and I'm not too fixated on the syntax of "FullChain"
. Any suggestion is appreciated.