Live data from Hacker News

Regex in Swift

benscheirman.com

31–37 of 37 posts

Re: Regex in Swift

#31
post #29
post #19

From the radar issue submission: > Any modern language should natively support regular expression literals Regex literals add needless complexity to the language, and tie it with a specific regex implementation, with no real benefit. Just because Perl/JS/Ruby have this kludge, doesn't mean a modern language "should" have it. Now, a way to write unescaped strings (e.g not having to escape all the regex operators like…

There are real benefits, they're called convenience and compatibility. And what's wrong with tying a language to a specific regex syntax? After all, you're also tying it top a specific outside-of-regexes syntax. Regexes are also code, they just happens to be written in a different sub-language than the rest of the program.

>There are real benefits, they're called convenience and compatibility.

Besides the fact that built-in regex literals are hardly any more "convenient" than a function call (a few keystrokes saved at best), regexes shouldn't be "convenient".

If anything, they should be discouraged. To quote JWZ: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions'. Now they have two problems."

Oh, and "compatibility" doesn't come to play at all. Why would regex literals be any more "compatible" (with what?) than a regex object/functions? Compatible to what? JS and Ruby syntax?

>And what's wrong with tying a language to a specific regex syntax? After all, you're also tying it top a specific outside-of-regexes syntax.

For one, because you have to keep maintaining it forever, as part of the core syntax, whereas with a library you could deprecate it. It's not like there aren't several regex flavors, and approaches on how execute them (e.g: http://swtch.com/~rsc/regexp/regexp1.html ).

Re: Regex in Swift

#32
post #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...

> Every major language has Regex support, often built-in

That's a very bold statement. My most commonly used languages (Haskell, Rust, Go, C, and C++) do not have regex support built in. I don't think any of them even have regex support in their standard library.

>I'd assume that a large percentage of programs employs Regexes somewhere

I think that would be incorrect. I've never used a regex in a production program. In fact, the only time I ever use regexes is when I'm scraping data or doing searches, and that certainly doesn't account for "a large percentage of programs".

A large percentage of programs also use URL fetching, but that's not a good reason to make URL fetching part of the core language.

Re: Regex in Swift

#33
post #32
post #5

Earlier quoted context omitted.

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...

> Every major language has Regex support, often built-in That's a very bold statement. My most commonly used languages (Haskell, Rust, Go, C, and C++) do not have regex support built in. I don't think any of them even have regex support in their standard library. >I'd assume that a large percentage of programs employs Regexes somewhere I think that would be incorrect. I've never used a regex in a production program.…

Rust, Go and C++ all have regex libraries as part of the standard library.

Re: Regex in Swift

#34
post #32

Earlier quoted context omitted.

> Every major language has Regex support, often built-in That's a very bold statement. My most commonly used languages (Haskell, Rust, Go, C, and C++) do not have regex support built in. I don't think any of them even have regex support in their standard library. >I'd assume that a large percentage of programs employs Regexes somewhere I think that would be incorrect. I've never used a regex in a production program.…

Rust, Go and C++ all have regex libraries as part of the standard library.

OK, cool. It's still not built in to the language in any meaningful way. There isn't any special syntax or rule-bending done for regexes.

Re: Regex in Swift

#35
post #27
post #18

Earlier quoted context omitted.

Having amazing string capabilities is more important now than ever. But that actually argues against including regex in the language syntax itself. You want regular expressions to be able to evolve to become ever more powerful and useful. So just include a literal string type in the language itself--one that minimizes the need for escapes and can be used for all sorts of protocols--and use a regex library. The syntax…

Aside from these reasons, you also have the case of performance optimization. In Perl, for example, pretty much all string parsing (that I've ever seen done in Perl code) is done via regular expressions. Regular expressions in Perl are such a thing that most software I've used that uses regular expressions uses the Perl-compatible regular expression library (libpcre). The issue is that if you provide developers with…

"more memory and more overhead than e.g. splitting a string by simply scanning it."

Regexes are simply scanning strings, they're just a more condensed syntax for specifying how the scanning should be done.

And in interpreted languages, using the built-in regex feature (which can apply high-level optimizations) will virtually always give much better performance than implementing the same logic by manually looping over the string character-by-character with the language's interpreted for/while loops.

Re: Regex in Swift

#36
post #31
post #29

Earlier quoted context omitted.

There are real benefits, they're called convenience and compatibility. And what's wrong with tying a language to a specific regex syntax? After all, you're also tying it top a specific outside-of-regexes syntax. Regexes are also code, they just happens to be written in a different sub-language than the rest of the program.

> There are real benefits, they're called convenience and compatibility. Besides the fact that built-in regex literals are hardly any more "convenient" than a function call (a few keystrokes saved at best), regexes shouldn't be "convenient". If anything, they should be discouraged. To quote JWZ: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions'. Now they have two problems." Oh…

Re "compatibility", I was thinking more about the language's library ecosystem, that could benefit from a single official regex implementation that can be relied upon and can be assumed to be known by programmers (when designing library APIs, for instance).

But I actually forgot to mention the biggest benefit of having dedicated regex literals (rather than just passing them as strings to some constructor at run-time):

They will be treated as part of the language, and can be parsed and compiled together with the rest of the code (a.k.a. at compile-time).

Which means:

1) Better performance

2) Syntax errors in regexes are compile-time errors, which is a huge help in keeping bugs out

3) Editors will show you syntax highlighting inside of regexes

It's hard to overstate the benefit of those three consequences; Once you've worked with a language that supports such compile-time regex literals (e.g. Perl) you don't want to go back to using regexes in a language that treats them as strings (e.g. Python).

Regexes are code, and defining code in strings to be eval'ed at run-time is just plain wrong.

Re: Regex in Swift

#37

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.

Rebol is another example worth looking at. It doesn't come with regex at all instead it uses a parse dialect (an embedded Top-down parsing language).

Some refs:

- http://www.rebol.com/docs/core23/rebolcore-15.html

- http://www.rebol.com/r3/docs/functions/parse.html

- http://www.codeconscious.com/rebol/parse-tutorial-r3.html

- https://en.wikibooks.org/wiki/REBOL_Programming/Language_Fea...

- https://en.wikipedia.org/wiki/Top-down_parsing_language

Post reply on HN