Providing a default type for a generic API

I wrote a pitch, years ago, for a feature that could fulfil this need: Pitch: Scoped Functions. 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 :stuck_out_tongue_winking_eye: In your BSON example, the pitch would let you write:

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.

1 Like