# \[Pitch\] Reflection

**URL:** <https://forums.swift.org/t/pitch-reflection/61438>\
**Category:** Pitches\
**Tags:** reflection\
**Created:** [November 14, 2022, 11:01pm UTC](https://forums.swift.org/t/pitch-reflection/61438 "2022-11-14T23:01:52Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [November 15, 2022, 6:22pm UTC](https://forums.swift.org/t/pitch-reflection/61438/12 "2022-11-15T18:22:45Z")

</div>

> [@saagarjha](#):
>
> there is no way to fundamentally prevent the creation of a `WwritableKeyPath` to a mutable field in the language

Yeah, I agree with you here. I think the thing we considered was that we don't want the easy API to be able to easily vend these mutable keypaths without any way of opting in/out.

> [@saagarjha](#):
>
> I think it would also be nice to have APIs that could do lookup by name, and (perhaps) by type. In particular these would be slightly faster than iterating the entire list of children for large types, which I have found to be a performance pitfall in several apps.

We could make an API to get a field from name, however the implementation would still need to iterate the list of fields to find the specific field (just like how one would do it now). However, unlike `Mirror`, iterating the collection of `Fields` is significantly faster because of some of the performance benefits I mentioned when comparing `Mirror` (and all of the collection wrappers are `RandomAccessCollection`). All of the APIs on `Field` are lazily retrieved unlike the children of `Mirror`. @Karoy_Lorentey mentioned an API synopsis for things like these collections wrappers that I would be more than happy to add.

> [@dnadoba](#):
>
> I'm wondering how this will play with variadic generics.  
> For example:
> 
> ```swift
> struct ABC<A..., B: Equatable> {
> struct XYZ<C: Hashable, D...> {}
> }
> 
> let partial = Type(ABC<Int, Int, Int>.XYZ<Int, Int>.self).partial 
> 
> ```
> 
> Does `create(with:)` support different shapes of A and D or just the shape specified during the creation of the partial type e.g. 2 for A and 1 for D in the example above?

A lot about variadic generics is still unknown at the runtime layer besides what @John_McCall posted here: [ABI for variadic generics](https://forums.swift.org/t/abi-for-variadic-generics/55213), so I'm unsure how it'll look and interact with the language at runtime for reflection purposes. That being said, I plan to support variadic generics however much I can with these new APIs (or propose new APIs if it makes sense).

---

_[View the full topic](https://forums.swift.org/t/pitch-reflection/61438)._
