Live data from Hacker News

Regex in Swift

benscheirman.com

1–10 of 37 posts

Re: Regex in Swift

#2
There is probably a good reason Swift doesn't have regex literals, regex operators, and other such things. These things are not common in statically, strongly typed languages with an emphasis on safety.

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.

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.

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.

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.

Re: Regex in Swift

#3
"Please tack on this random application-specific feature that I like"

Can we add supprt for overloading the comma operator? How about the mail function from PHP?

Re: Regex in Swift

#4

There is probably a good reason Swift doesn't have regex literals, regex operators, and other such things. These things are not common in statically, strongly typed languages with an emphasis on safety. 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 a…

I don't think regexp support has a lot to do with being "stringly typed". In fact, regular expressions are one of the best tools to avoid that antipattern in scripting languages, because they allow you to easily analyze string input and construct an internal representation from it. I think it's more that most of the scripting languages originally grew around the task of text processing (e.g. as system scripts), so their facilities for dealing with text are great. Languages like Java, C#, Go, Haskell, etc., had more of a focus on general computing.

Re: Regex in Swift

#5
post #3

"Please tack on this random application-specific feature that I like" Can we add supprt for overloading the comma operator? How about the mail function from PHP?

How are regular expressions "random" and "application-specific"? Admittedly I do not have a source for this, but I'd assume that a large percentage of programs employs Regexes somewhere. Every major language has Regex support, often built-in [1].

Swift, among other things, aims to make things less verbose right?

[1]: http://en.wikipedia.org/wiki/Comparison_of_regular_expressio...

Re: Regex in Swift

#6
post #3

"Please tack on this random application-specific feature that I like" Can we add supprt for overloading the comma operator? How about the mail function from PHP?

It could be less specific as a raw string literal, like python r"".

Re: Regex in Swift

#8

There is probably a good reason Swift doesn't have regex literals, regex operators, and other such things. These things are not common in statically, strongly typed languages with an emphasis on safety. 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 a…

I think the reason is that it's a brand new language that has unimplemented and missing features. Regex as a first-class citizen seems like it absolutely fits into the goals that Swift has and I would be very surprised if its long-term exclusion is intended.

Re: Regex in Swift

#9

There is probably a good reason Swift doesn't have regex literals, regex operators, and other such things. These things are not common in statically, strongly typed languages with an emphasis on safety. 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 a…

I think in the specific case of Haskell, parser combinator libraries like Parsec are so nice that it can be more convenient to use parser combinators than regexes for parsing regular languages. I'm haven't seen similar substitutions in other languages.

Re: Regex in Swift

#10
post #8

There is probably a good reason Swift doesn't have regex literals, regex operators, and other such things. These things are not common in statically, strongly typed languages with an emphasis on safety. 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 a…

I think the reason is that it's a brand new language that has unimplemented and missing features. Regex as a first-class citizen seems like it absolutely fits into the goals that Swift has and I would be very surprised if its long-term exclusion is intended.

I agree more with the parent on this one. I would actually expect GCD to receive first class language features before regex. There are certain operations like @sync that made their way into objective-c and Swift can use some language features to implement very common patterns in iOS/OS X programming.

Then again, in Swift you can declare your own operator and call into NSRegularExpression.

Post reply on HN