# Providing a default type for a generic API

**URL:** <https://forums.swift.org/t/providing-a-default-type-for-a-generic-api/70927>\
**Category:** Using Swift\
**Created:** [March 26, 2024, 10:49pm UTC](https://forums.swift.org/t/providing-a-default-type-for-a-generic-api/70927 "2024-03-26T22:49:35Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [March 27, 2024, 1:33pm UTC](https://forums.swift.org/t/providing-a-default-type-for-a-generic-api/70927/4 "2024-03-27T13:33:48Z")

</div>

I wrote a pitch, years ago, for a feature that could fulfil this need: [Pitch: Scoped Functions](https://forums.swift.org/t/pitch-scoped-functions/45486). The pitch was not very well written, so I'm not surprised it didn't fly.

The goal was let an API favor one specific value for the leading dot syntax, inside the body of a closure, or... in an autoclosure 😜 In your BSON example, the pitch would let you write:

```swift
extension BSON.DocumentEncoder
{
  @inlinable public
  subscript(
    with key:@autoclosure @scoped (CodingKey.Type) -> some RawRepresentable<String>
  ) -> BSON.FieldEncoder { ... }
}

enum MyEnum: String { case bar }

encoder[.foo] // OK: CodingKey.foo
encoder[MyEnum.bar] // OK: MyEnum

```

EDIT: fixed the sample code so that `.foo` is understood as `CodingKey.foo` by scoping the `key` to `CodingKey.Type`.

---

_[View the full topic](https://forums.swift.org/t/providing-a-default-type-for-a-generic-api/70927)._
