I'm trying to construct a recursive, strongly-typed-as-in-SwiftUI type from a parameter pack, without resorting to type erasers.
I'm working with a resultBuilder that has the following functions supported:
static func buildBlock<T>(_ components: T) -> T where T: Foo
static func buildEither<T, U>(first component: T) -> EitherFoo<T, U> where T: Foo, U: Foo
static func buildEither<T, U>(second component: U) -> EitherFoo<T, U> where T: Foo, U: Foo
It does not support a parameter pack buildBlock by design, the intention is that you can return one instance from it, no more, no less.
There is no AnyFoo, and a good solution to this problem won't involve adding it.
I have a struct FooCollection declared as follows.
struct FooCollection<each T>: Foo where T: Foo {
/*
initializer elided, but this is intended to be initialized with like the following:
FooCollection {
MyFooProducer()
MyBarProducer()
}
and the "producers" create a dependency for an associated `Foo` later. There is never more than one created at once.
*/
var members: (repeat Optional<each T>)
// note; `content` is a requirement of the `Foo` protocol, and `FooCollection` is itself a subtype of `Foo`.
var content: some Foo {
// ??
}
}
My problem is I cannot figure out how to fill in body. Pack Iteration doesn't seem like the answer because I can't guarantee that there are 1 or more members in each T, and recursion doesn't work for the same reason; I can't find a way to destructure the pack in a way that moves me closer to the goal either (I thought there was a way to do this in the past, but can't figure out how it would ever have worked).
I'm leaning toward this not being possible b/c I can't even figure out how I would spell such a type if opaque types didn't exist, but thought I'd ask in case anyone has any ideas.