<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">@Eneko While it sure seems possible to specify the type, I think this would go against the salient point "If something’s worth capturing, it’s worth giving it a name.” Putting the name further away seems like a step backward.<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(40, 43, 53); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: "Helvetica Neue"; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">I could imagine a slightly more succinct syntax where things like .numberFromDigits are replaced by protocol conformance of the bound type:</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: "Helvetica Neue"; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">```</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(147, 201, 106); background-color: rgb(40, 43, 53);" class="">extension Int: Regexable {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(147, 201, 106); background-color: rgb(40, 43, 53);" class=""> func baseRegex<T>() -> Regex<T, Int></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(147, 201, 106); background-color: rgb(40, 43, 53);" class="">}</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(147, 201, 106); background-color: rgb(40, 43, 53);" class="">let usPhoneNumber = (/let area: Int/.exactDigits(3) + "-").oneOrZero +</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(147, 201, 106); background-color: rgb(40, 43, 53);" class=""> /let routing: Int/.exactDigits(3) + "-" +</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(147, 201, 106); background-color: rgb(40, 43, 53);" class=""> /let local: Int/.exactDigits(4)</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: "Helvetica Neue"; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">```</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(40, 43, 53); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: "Helvetica Neue"; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">In this model, the <span style="color: #68878f" class="">`</span><span style="font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(147, 201, 106);" class="">//</span><span style="color: #68878f" class="">`</span> syntax will only be used for initial binding and swifty transformations will build the final regex.</div><div class=""><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class="">On Jan 16, 2018, at 9:20 AM, Eneko Alonso 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 style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Could it be possible to specify the regex type ahead avoiding having to specify the type of each captured group?<div class=""><br class=""></div><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font color="#4f8187" face="Menlo" class=""><span style="font-size: 11px;" class="">let usPhoneNumber</span></font><span style="color: rgb(79, 129, 135); font-family: Menlo; font-size: 11px;" class="">: </span><span style="color: rgb(79, 129, 135); font-family: Menlo; font-size: 11px;" class="">Regex<UnicodeScalar,</span><span style="color: rgb(79, 129, 135); font-family: Menlo; font-size: 11px;" class=""> </span><span style="color: rgb(79, 129, 135); font-family: Menlo; font-size: 11px;" class="">(area: Int?, routing: Int, local: Int)></span><span style="font-size: 11px; color: rgb(79, 129, 135); font-family: Menlo;" class=""> = /</span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);"><font color="#4f8187" face="Menlo" class=""><span style="font-size: 11px;" class=""> (\d{3}?) -<br class=""> (\d{3}) -<br class=""> (\d{4}) /<br class=""></span></font><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);">“Verbose” alternative:</div></div></div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);">let usPhoneNumber: Regex<UnicodeScalar, (area: Int?, routing: Int, local: Int)> = / </div><div class="" style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);"> .optional(.numberFromDigits(.exactly(3)) + "-“) +</div><div class="" style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);"> .numberFromDigits(.exactly(3)) + "-"</div><div class="" style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);"> .numberFromDigits(.exactly(4)) /</div><div class="" style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);">print(type(of: usPhoneNumber)) // => Regex<UnicodeScalar, (area: Int?, routing: Int, local: Int)></div></div><div class=""><br class=""><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Eneko</div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jan 16, 2018, at 8:52 AM, George Leontiev 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 style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">Thanks, Michael. This is very interesting!</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">I wonder if it is worth considering (for lack of a better word) <span style="color: #957e35" class="">*</span><i class="">verbose</i><span style="color: #957e35" class="">*</span> regular expression for Swift.</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">For instance, your example:</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class="">let usPhoneNumber = /</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> (let area: Int? <- \d{3}?) -</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> (let routing: Int <- \d{3}) -</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> (let local: Int <- \d{4}) /</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">would become something like (strawman syntax):</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class="">let usPhoneNumber = /let area: Int? <- .numberFromDigits(.exactly(3))/ + "-" +</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> /let routing: Int <- .numberFromDigits(.exactly(3))/ + "-"</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> /let local: Int <- .numberFromDigits(.exactly(4))/</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">With this format, I also noticed that your code wouldn't match "555-5555", only "-555-5555", so maybe it would end up being something like:</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class="">let usPhoneNumber = .optional(/let area: Int <- .numberFromDigits(.exactly(3))/ + "-") +</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> /let routing: Int <- .numberFromDigits(.exactly(3))/ + "-"</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> /let local: Int <- .numberFromDigits(.exactly(4))/</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">Notice that <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">area</span><span style="color: #957e35" class="">`</span> is initially a non-optional <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">Int</span><span style="color: #957e35" class="">`</span>, but becomes optional when transformed by the <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">optional</span><span style="color: #957e35" class="">`</span> directive.</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">Other directives may be:</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class="">let decimal = /let beforeDecimalPoint: Int <-- .numberFromDigits(.oneOrMore)/ +</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> .optional("." + /let afterDecimalPoint: Int <-- .numberFromDigits(.oneOrMore)/</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">In this world, the <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">/<--/</span><span style="color: #957e35" class="">`</span> format will only be used for explicit binding, and the rest will be inferred from generic <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">+</span><span style="color: #957e35" class="">`</span> operators.</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">I also think it would be helpful if <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">Regex</span><span style="color: #957e35" class="">`</span> was generic over all sequence types.</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">Going back to the phone example, this would looks something like:</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class="">let usPhoneNumber = .optional(/let area: Int <- .numberFromDigits(.exactly(3))/ + "-") +</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> /let routing: Int <- .numberFromDigits(.exactly(3))/ + "-"</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""> /let local: Int <- .numberFromDigits(.exactly(4))/</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class="">print(type(of: usPhoneNumber)) // => Regex<UnicodeScalar, (area: Int?, routing: Int, local: Int)></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">```</div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue"; background-color: rgb(255, 255, 255);" class="">Note the addition of <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">UnicodeScalar</span><span style="color: #957e35" class="">`</span> to the signature of <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">Regex</span><span style="color: #957e35" class="">`</span>. Other interesting signatures are <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">Regex<JSONToken, JSONEnumeration></span><span style="color: #957e35" class="">`</span> or <span style="color: #957e35" class="">`</span><span style="font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">Regex<HTTPRequestHeaderToken, HTTPRequestHeader></span><span style="color: #957e35" class="">`</span>. Building parsers becomes fun!</div><div class=""><br class=""></div><div class="">- George</div><div class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jan 10, 2018, at 11:58 AM, Michael Ilseman 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=""><div class="">Hello, I just sent an email to swift-dev titled "State of String: ABI, Performance, Ergonomics, and You!” at <a href="https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20180108/006407.html" class="">https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20180108/006407.html</a>, whose gist can be found at <a href="https://gist.github.com/milseman/bb39ef7f170641ae52c13600a512782f" class="">https://gist.github.com/milseman/bb39ef7f170641ae52c13600a512782f</a>. I posted to swift-dev as much of the content is from an implementation perspective, but it also addresses many areas of potential evolution. Please refer to that email for details; here’s the recap from it:<br class=""><br class="">### Recap: Potential Additions for Swift 5<br class=""><br class="">* Some form of unmanaged or unsafe Strings, and corresponding APIs<br class="">* Exposing performance flags, and some way to request a scan to populate them<br class="">* API gaps<br class="">* Character and UnicodeScalar properties, such as isNewline<br class="">* Generalizing, and optimizing, String interpolation<br class="">* Regex literals, Regex type, and generalized pattern match destructuring<br class="">* Substitution APIs, in conjunction with Regexes.<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>