Type Eraser for Container

Hi all,

Is this allowed:

environment(\.container, MyContainer() as Any)

While injecting Model container into the environment we use Type . Is there any universal injection type that can inject container as set of any types rather than array similar to navigationPath() type-eraser ?

Type eraser will most likely be done at the .container Environment level using generic or plain protocols. As you have done above, you pass in concrete type and then access it like @environment(.container) var container.

I’m referring to the injection of a model container into the environment. We accomplish this by declaring the type as follows:

.modelContainer(for: [Book.self, Author.self])

For instance:

struct Example: App {
    @State private var appData = ApplicationData.shared

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(appData)
                .modelContainer(for: [Book.self, Author.self])
        }
    }
}

I was wondering if we could implement a feature similar to the navigationPath() type-erasing functionality. This allows us to define a heterogeneous set of types, as we can do with:

@State private var path = NavigationPath()

Instead of explicitly declaring:

@State private var path = [Book] ()

Is there a way to build ( if not already a way) into Swift Language a similar capability for environment injection, enabling the injection of a heterogeneous set of types without needing to declare the type explicitly?

I am not sure exactly what you are asking but as far as the ModelContainer yo must supply all your model types, directly or indirectly, when you create the container object

Type Eraser for Container
 
 
Q