There is probably a good reason Swift doesn't have regex literals, regex operators, and other such things. These things are <i>not common</i> in statically, strongly typed languages with an emphasis on safety.<p>That could be pure correlation. Perhaps it is just coincidence that JavaScript, PHP, Perl, and a handful of others happen to have a lot of "stringly typed" code, message passing using strings as data structures, and an emphasis on string and array operations.<p>Or it could be that such features, due to their ease of use and the turing-tarpit of powerful enhanced regular expression languages, developers fall into the allure and trap of stringly typed code. And safe languages are the languages that don't make it exceedingly easy to use regular expressions. Because there is no reason you cannot use regular expressions in safer languages like Java, C#, Go, Rust, Haskell, or to be charitable, C++. It just isn't a first-class citizen in those languages.<p>I'm of the view that if you make your language such that idioms prone to error and bad practices easy, then developers will be prone to error and bad practices.<p>tl;dr: Even as someone not invested in iOS or OS X development at all, given the chance I'd veto these features. Give me types, not strings.
The author is maybe not aware that there's already a convenience form that doesn't require explicitly making an NSRegularExpression object.<p><pre><code> if name.rangeOfString("ski$", options: .RegularExpressionSearch).location != NSNotFound {
println("\(name) is probably polish")
}
</code></pre>
That's existing Cocoa API; in Swift (hopefully!) the API can be updated to return nil if there's no match, so that it can read<p><pre><code> if let match = name.rangeOfString("ski$", options: .RegularExpressionSearch) {
println("\(name) is probably polish")
}
</code></pre>
which I don't think is too bad!
This feature is crying out for procedural macro support, not for being built into the language. For comparison, Rust has compile-time regular expressions (which, I will note, this blog post does not do; it's all runtime-parsed), implemented as a separate library that ships with Rust, using the procedural macro support (also called syntax extensions). This means the compiler and the language spec knows _nothing_ about regular expressions, and only the library libregex knows anything about it, and if you don't link against libregex, your program has no knowledge about it.<p>This ends up looking like the following:<p><pre><code> #![feature(phase)] // feature-gate for syntax extensions
#[phase(plugin)] // tells the compiler the following crate has syntax extensions
extern crate regex_macros; // a crate is a rust library. this one provides the syntax extension
extern crate regex; // this one provides the runtime support for regular expressions
fn main() {
let re = regex!(r"^\d{4}-\d{2}-\d{2}$"); // compile-time regular expression
assert_eq!(re.is_match("2014-01-01"), true);
}
</code></pre>
That `regex!(...)` call will trigger the compile-time syntax extension to parse the regular expression, throw a compile-time error if the parsing fails, and otherwise expand to an inline data structure that contains the runtime representation of the parsed regular expression. Even better, it generates native Rust code for various bits of the matching process, instead of relying on the generalized implementation used for runtime-parsed regular expressions, which means it's actually faster to use a compile-time regex. The downside is, of course, that it's generating specialized code for each one, so this can bloat your binary if you use a lot of regular expressions, but on the upside turning on Link-Time Optimization can get rid of a lot of this overhead.
From the radar issue submission:<p>><i>Any modern language should natively support regular expression literals</i><p>Regex literals add needless complexity to the language, and tie it with a specific regex implementation, with no real benefit.<p>Just because Perl/JS/Ruby have this kludge, doesn't mean a modern language "should" have it.<p>Now, a way to write unescaped strings (e.g not having to escape all the regex operators like \ etc), that I can stand behind.
How about something like <a href="http://www.inf.puc-rio.br/~roberto/lpeg/" rel="nofollow">http://www.inf.puc-rio.br/~roberto/lpeg/</a> ?<p>Since the optional pattern syntax doesn't use backslashes, they aren't a problem. I'd hack this up myself if Swift weren't Apple-only.
"Please tack on this random application-specific feature that I like"<p>Can we add supprt for overloading the comma operator? How about the mail function from PHP?