[swift-evolution] Empower String type with regular expression

Chris Lattner clattner at apple.com
Tue Feb 2 23:29:54 CST 2016


> On Feb 1, 2016, at 9:44 PM, Thorsten Seitz via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Something like Scala's extractors or F#'s Active Patterns would be most welcome to generalize pattern matching.
> 
> http://docs.scala-lang.org/tutorials/tour/extractor-objects.html <http://docs.scala-lang.org/tutorials/tour/extractor-objects.html>
> https://en.m.wikibooks.org/wiki/F_Sharp_Programming/Active_Patterns <https://en.m.wikibooks.org/wiki/F_Sharp_Programming/Active_Patterns>
This is pretty interesting, and I agree with the general observation here: there is a duality between printing and parsing that should be modeled and exploited.

One of the things that we discussed in the time leading up to Swift 1 (but then pushed off and never came back to) was model for doing standardized formatted printing - capturing the power and expressiveness of printf, without the problems it entails.  A sketch of the proposal is here:
https://github.com/apple/swift/blob/master/docs/TextFormatting.rst <https://github.com/apple/swift/blob/master/docs/TextFormatting.rst>

The pertinent idea here is that types can have a default printing rule (e.g. integers print in decimal) but then can opt into providing a more powerful formatting model by supporting modifier characters (for example “x” for an integer could mean “hexadecimal”).  This would be protocol based, so arbitrary types could participate.

If you bring the same concept to regex parsing, I think it would make a lot of sense for primitive types to support “default” regex rules (e.g. integers would default to /[0-9]+/ ) and then have modifier characters that support other standard modes for them (e.g. x for hexadecimal).  This would obviously want to be extensible to arbitrary types, so that (e.g.) NSDate could support the format families that make sense.

Going with this would address one of my major gripes with regex’s used in practice, which is that they are a huge violation of the DRY principle, and that reuse of regex patterns almost never happens.

-Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160202/40320666/attachment.html>


More information about the swift-evolution mailing list