[swift-evolution] Empower String type with regular expression
Patrick R. Gili
gili.patrick.r at gili-labs.com
Sun Feb 7 16:05:29 CST 2016
I have been reading the documentation and design notes for regular expressions in Perl6. Thank you for the pointer to the documentation. I really like what the Perl6 community has done with regular expressions (I like the notion of grammars too).
Given the insights you shared below, I imagine we can address your gripe with regular expressions by implementing something like Perl6's named regular expressions, which easily facilitates reuse.
To further encourage reuse, we could introduce the notion of "standardized parsing" of data types, much in the same way that Swift already supports the notion of "standardized formatted printing".
To achieve this, we could introduce a protocol, similar to CustomStringConvertible (or maybe extend CustomStringConvertible) that would not define a method to convert a string to the data type, but define the regular expression that facilitates the conversion. The protocol could also facilitate the reuse of the regular expression in other regular expression literals.
Sent from my iPad Pro
> On Feb 3, 2016, at 12:29 AM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>> 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.
> 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:
> 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.
> swift-evolution mailing list
> swift-evolution at swift.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution