Inconsistance throwing behavior on String and Regex's xxMatch

We have public func wholeMatch(in s: String) throws -> Regex<Output>.Match? method on Regex and public func wholeMatch<R>(of r: R) -> Regex<R.RegexOutput>.Match? where R : RegexComponent on String.

Their usage is something like the following and IMO they can exchange in some extend.

let regex = /Hello/
let input = "Hello world"

try regex.wholeMatch(in: input)
input.wholeMatch(of: regex) // The result is the same as the above

So my question is why one is marked as "throw" and the other is not. Should the throwing behavior be aligned?

Apologies if I missed something already discussed before.

The core regex operations can throw, because things like custom regex consumers can throw their own custom errors. Regex's hosted API will faithfully surface these failure reasons rather than squash them all into a single notion of failure via nil. This is useful for libraries and more advanced use cases.

The common case is to not care about how something failed to match. The API on string are collection algorithms that will coalesce all failure reasons into nil, meaning it failed. This is more convenient as the caller of these higher level operations care about match-or-not. If these threw, then all the call sites would be annotated with extra try? ceremony for little benefit.

5 Likes