It’s good to see the addition of APIs for accessing captures by name with AnyRegexOutput. I would have liked to have extra introspection APIs on Regex<AnyRegexOutput> to find out the names/numbers of captures (as discussed in the pitch thread), but appreciate that this is something that can be added later. The use case I see for this is for implementing search with user defined regexes, with either pre-defined behaviour for capture group names, or offering 'replace' functionality that can use capture groups.
I asked in the pitch thread about whether names mattered when converting to strongly typed output (example changed here to cast Regex, not Regex.Match, assuming the same rules would apply to both).
let regex = try! Regex("(?<name>abc)(de)")
let typed1 = regex.as((Substring, name: Substring, Substring).self) // Can cast with names?
let typed2 = regex.as((Substring, Substring, Substring).self) // Can cast without names?
let typed3 = regex.as((whole: Substring, foo: Substring, bar: Substring).self) // Can cast with different names?
Taking this further, what happens with roundtripping via a strongly typed Output? Does it use the name from the original regex, or the name of the typed regex?
let regex = try! Regex("(?<name>abc)(de)")
let typed = regex.as((whole: Substring, foo: Substring, bar: Substring).self)
let regex2 = Regex(typed)
regex2.contains(captureNamed: "name") // Uses original names?
regex2.contains(captureNamed: "foo") // Uses strongly typed names?
How is type compatibility handled for AnyRegexOutput to typed conversions of Regex/Match? eg
let regex = try! Regex("(abc)")
let typed1 = regex.as((Substring, Substring).self) // 'Intrinsic' type - succeeds
let typed2 = regex.as((Substring, Substring?).self) // OK? `Substring?` is a subtype of `Substring`
// Similarly for protocols for transformed capture types:
// (Substring, Foo) -> AnyRegexOutput -> (Substring, any Fooable)
As a more general point, it would be helpful to have some examples in the proposal of using AnyRegexOutput, with matches with quantified captures (abc)+, conversions, interaction with literals and the DSL.