# Type Eraser for Containers

**URL:** <https://forums.swift.org/t/type-eraser-for-containers/75710>\
**Category:** Using Swift\
**Tags:** swiftdata\
**Created:** [October 30, 2024, 3:18pm UTC](https://forums.swift.org/t/type-eraser-for-containers/75710 "2024-10-30T15:18:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikolaknez](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikolaknez/32/44064_2.png) [@nikolaknez](https://forums.swift.org/u/nikolaknez)\
**Post date:** [October 30, 2024, 3:18pm UTC](https://forums.swift.org/t/type-eraser-for-containers/75710/1 "2024-10-30T15:18:47Z")

</div>

Hi all,

Is this allowed:

```swift
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 ?

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [October 30, 2024, 5:15pm UTC](https://forums.swift.org/t/type-eraser-for-containers/75710/2 "2024-10-30T17:15:19Z")

</div>

You've left a lot of context out, so I'm not quite sure what you're asking. You might have more luck asking on the [Apple developer forums](https://forums.developer.apple.com), since I think this might be specific to Apple's platform frameworks.

---

<div class="post-metadata">

**Author:** ![nikolaknez](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikolaknez/32/44064_2.png) [@nikolaknez](https://forums.swift.org/u/nikolaknez)\
**Post date:** [October 30, 2024, 8:45pm UTC](https://forums.swift.org/t/type-eraser-for-containers/75710/3 "2024-10-30T20:45:03Z")

</div>

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

swift

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

```

For instance:

swift

```swift
@main
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:

swift

```swift
@State private var path = NavigationPath()

```

Instead of explicitly declaring:

swift

```swift
@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?

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [October 31, 2024, 8:39am UTC](https://forums.swift.org/t/type-eraser-for-containers/75710/4 "2024-10-31T08:39:14Z")

</div>

You'll probably have better luck asking over at [Apple Developer Forums](https://developer.apple.com/forums/) since your question is about Apple's SDKs.

Although Swift undoubtedly is tightly connected with Apple and their platforms, it is also a stand-alone project which supports many non-Apple platforms. This forum is about Swift as a language and compiler, its features, and future directions.

For questions about how to program applications using Swift on Apple platforms, the Apple Developer site is better suited.
