Skip to content

Optional transformed capture has incorrect type when nil #827

Closed

Description

@natecook1000

When a RegexBuilder pattern includes an optional, transformed capture, and the capture is nil, converting the match to AnyRegexOutput and then accessing the capture's type shows Substring? instead of the transformed type.

let pattern = Regex {
    Capture(OneOrMore(.digit)) { Int($0)! }
    Optionally {
        "."
        Capture(OneOrMore(.digit)) { Int($0)! }
    } 
}

for str in ["123.456", "789"] {
    if let match = str.wholeMatch(of: pattern) {
        print(match.output)
        let erasedMatch = Regex<AnyRegexOutput>.Match(match)
        print(erasedMatch[1].type, erasedMatch[2].type)
    }
}

// Output:
// ("123.456", 123, Optional(456))
// Int Optional<Int>
// ("789", 789, nil)
// Int Optional<Substring>

Activity

  1. natecook1000 commented on Sep 30, 2025

    @natecook1000
    MemberAuthor

    Fixed by #825.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions