<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 1, 2016, at 9:44 PM, Thorsten Seitz via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><span class=""></span></div><div class=""><div class=""></div><div class="">Something like Scala's extractors or F#'s Active Patterns would be most welcome to generalize pattern matching.</div><div class=""><br class=""></div><div class=""><a href="http://docs.scala-lang.org/tutorials/tour/extractor-objects.html" class="">http://docs.scala-lang.org/tutorials/tour/extractor-objects.html</a></div><div class=""><a href="https://en.m.wikibooks.org/wiki/F_Sharp_Programming/Active_Patterns" class="">https://en.m.wikibooks.org/wiki/F_Sharp_Programming/Active_Patterns</a></div></div></div></div></blockquote><br class=""></div><div>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.</div><div><br class=""></div><div>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:</div><div><a href="https://github.com/apple/swift/blob/master/docs/TextFormatting.rst" class="">https://github.com/apple/swift/blob/master/docs/TextFormatting.rst</a></div><div><br class=""></div><div>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.</div><div><br class=""></div><div>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.</div><div><br class=""></div><div>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.</div><div><br class=""></div><div>-Chris</div><br class=""></body></html>