While I'm sympathetic toward the goal of delivering regexes with the simplest and most recognizable literal syntax, I do find some of the opposing arguments compelling. So I was wondering how the "only #/../#
extended literals" alternative would look.
// short regex literals
let regex = Regex {
Capture { #/[$£]/# }
TryCapture {
#/\d+/#
"."
#/\d{2}/#
} transform: {
Amount(twoDecimalPlaces: $0)
}
}
// medium
let regex = /([ab])|\d+/
let regex = #/([ab])|\d+/#
// long+
let regex = /(?<identifier>[[:alpha:]]\w*) = (?<hex>[0-9A-F]+)/
let regex = #/(?<identifier>[[:alpha:]]\w*) = (?<hex>[0-9A-F]+)/#
My opinion is that the wrapping #'s hardly make a difference to the legibility of any individual regex literal. Short ones remain relatively short and simple, long ones remain long and obtuse.
However, in cases where many (probably short) literals are used, such as with the regex DSL, the sheer number of #'s looks a bit jarring to me. Maybe with the right syntax highlighting theme it would be fine (e.g. with a subtle gray on the #'s).
Also, I imagine that typing #/
in a good editor should autocomplete /#
ahead of the cursor, making it that much easier to type (although this is NOT the case today with raw strings in Xcode and VS Code!).
Of course, the other big issue is that the extended literal syntax strongly suggests existence of the bare version. Perhaps even so much that even if the bare syntax were rejected, library authors might from now on avoid using prefix /
operators, considering its future uncertain, which would in turn give the space to bare regex literals to eventually justify the breakage?
What about allowing /
to be wrapped in backticks to disambiguate it as an operator rather than the start delimiter of a bare regex literal?
prefix func / (...) -> ...
let casepath = `/`Enum.a // parse error today
Similar to:
func await (...) -> ...
`await`(...) // OK
Not great, but also not that bad? It would still cause a source break but would allow continued use of an operator with semblance to the backslash. Perhaps I'm missing something obvious as to why this is not already allowed today.