<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="">Howdy,<div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>Yes, I was also intrigued by the “Regex” validation mentioned in another post. &nbsp;It could offer a convenient way to get some literals support in without the headaches associated with the constexpr C++ approach.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>I’m curious, though, how many types can we image in can be validated by this method? &nbsp;If it really is just URL’s, then I’d actually lean towards making this a compiler magic feature.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>Someone else mentioned fetching the URL’s for a preview. &nbsp;Given that we might be coding “deletes” in URL’s (yes, I recently met a backend developer who coded a delete as a GET), I really highly suggest we not ping people’s API’s artificially. &nbsp;At least we shouldn’t for non-file-scheme URLs. &nbsp;IMHO, verifying that a service is active isn’t really the Swift compiler’s job. &nbsp;It might happen as part of coordinated run-time tests, which sometimes have to be balanced to keep test data correct, something the IDE wouldn’t know how to enforce correctly.</div><div class="">-Ben</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 19, 2016, at 1:41 AM, David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 17, 2016, at 1:12 PM, Micah Hainline via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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=""></div><div class="">I'd love a fleshed out elegant example for URL that shows what a complete implementation of that special init method would look like.&nbsp;</div></div></div></blockquote><br class=""></div><div class="">Sorry this took so long… the weekend kinda got away from me.</div><div class=""><br class=""></div><div class="">Anyway, I was thinking something like this (which has been very simplified on account of my regexing being sub-sketchy, and me not knowing exactly what’s valid in an URL anyway):</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""><font color="#bb2ca2" class="">#literalpatterns</font>&nbsp;+= (</span><font face="Menlo" class="">name:&nbsp;</font><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">“URLLiteralType</span><font color="#d12f1b" face="Menlo" class="">”</font><font face="Menlo" class="">, components</font><font face="Menlo" class="">: (name: url, type:&nbsp;</font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">pattern: </span><font color="#d12f1b" face="Menlo" class="">“(http|https)://</font><font color="#d12f1b" face="Menlo" class="">(www.)?[a-z|A-Z|0-9]+</font><font color="#d12f1b" face="Menlo" class="">.(com|org|net)(/</font><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">[a-z|A-Z|0-9]+)*(/</span><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">[a-z|A-Z|0-9]+.</span><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">[a-z|A-Z|0-9]+)?</span><font face="Menlo" class=""><font color="#d12f1b" class="">”</font>), </font><font face="Menlo" class="">protocol:&nbsp;</font><span style="color: rgb(79, 129, 135); font-family: Menlo;" class="">ExpressibleByURLLiteral</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">)</span></div></div></blockquote><div class=""><div class=""><div style="margin: 0px; line-height: normal;" class="">This would let the compiler know pretty much everything it needs to know… that the “new” type is called “URLLiteralType", that it starts out life as young StringLiteralType with a bright future in the computer industry, that in order to succeed it has to match a given pattern, and what protocol a type has to conform to in order to use an URLLiteral. In practice, the compiler would synthesize a struct containing the specified members and validate the literal with the specified pattern before making an “instance” of it (since we’re talking about literals and compile-time code here, I’m pretty sure that “instance" the wrong terminology… pardon my ignorance)</div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> URLLiteralType: {</span></div></div></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> url: </span><span style="font-variant-ligatures: no-common-ligatures" class="">StringLiteralType</span></div></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div></div></blockquote><div class=""><div style="margin: 0px; line-height: normal;" class=""><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">A tuple would be better, IMHO, but according to the playground, single-element tuples can’t have element labels.&nbsp;</span>As for the implementation of the init function:</div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(urlLiteral value:&nbsp;<span style="color: rgb(79, 129, 135);" class="">URLLiteralType</span>) {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> urlString = value.url</span></div></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><font color="#008400" face="Menlo" class="">//Do whatever URL is doing now, except there’s no need to check for errors since the compiler pre-validated it for us</font></div></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div></div></div></blockquote><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><br class=""></div></div><div class="">If it’d be more useful, the pattern could be split into multiple pieces:</div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""><font color="#bb2ca2" class="">#literalpatterns</font>&nbsp;+= (</span><font face="Menlo" class="">name:&nbsp;</font><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">“URLLiteralType</span><font color="#d12f1b" face="Menlo" class="">”</font><font face="Menlo" class="">,</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;components: (</font><font face="Menlo" class="">(name:&nbsp;</font><font color="#d12f1b" face="Menlo" class="">“`protocol`"</font><font face="Menlo" class="">, type:&nbsp;</font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">pattern:&nbsp;</span><font color="#d12f1b" face="Menlo" class="">“(http|https)”</font><font face="Menlo" class="">),</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (name:&nbsp;</font><span style="color: rgb(187, 44, 162); font-family: Menlo;" class="">_</span><font face="Menlo" class="">, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type:&nbsp;</font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">pattern:&nbsp;</span><font color="#d12f1b" face="Menlo" class="">“://”</font><font face="Menlo" class="">),</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (name:&nbsp;</font><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">“domain"</span><font face="Menlo" class="">, &nbsp; &nbsp;&nbsp;</font><font face="Menlo" class="">type:&nbsp;</font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">pattern:&nbsp;</span><font color="#d12f1b" face="Menlo" class="">“(www.)?[a-z|A-Z|0-9]+</font><font color="#d12f1b" face="Menlo" class="">.(com|org|net)</font><font face="Menlo" class=""><font color="#d12f1b" class="">”</font>),</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (name:&nbsp;</font><font color="#d12f1b" face="Menlo" class="">“path”</font><font face="Menlo" class="">, &nbsp; &nbsp; &nbsp;&nbsp;</font><font face="Menlo" class="">type:&nbsp;</font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">pattern: </span><font color="#d12f1b" face="Menlo" class="">"(/</font><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">[a-z|A-Z|0-9]+)*(/</span><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">[a-z|A-Z|0-9]+.</span><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">[a-z|A-Z|0-9]+)?</span><font face="Menlo" class=""><font color="#d12f1b" class="">”</font>))</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;protocol:&nbsp;</font><span style="color: rgb(79, 129, 135); font-family: Menlo;" class="">ExpressibleByURLLiteral</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">)</span></div></div></blockquote><div class=""><div style="margin: 0px; line-height: normal;" class="">This would result in URLLiteralType looking like this:</div><div style="margin: 0px; line-height: normal;" class=""><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;URLLiteralType: {</span></div></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;`protocol`:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures;" class="">StringLiteralType</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;domain:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures;" class="">StringLiteralType</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">&nbsp; &nbsp; let</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;path:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures;" class="">StringLiteralType</span></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class="">}</div></div></div></blockquote>And in the init would start out like this:</div><div style="margin: 0px; line-height: normal;" class=""><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(urlLiteral value:&nbsp;<span style="color: rgb(79, 129, 135);" class="">URLLiteralType</span>) {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> protocolType = value.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">protocol</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> domain = value.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">domain</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;path = value.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">path</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><font color="#008400" face="Menlo" class="">//Do whatever with the components</font></div></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div></div></div></blockquote></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">The “base” types of literals like Int or String that don’t refine pre-existing literal types would still need a bit of compiler magic (or at least a different mechanism for becoming actual types), but&nbsp;</span>as long as a type doesn’t take advantage of reference semantics in its stored properties or something, I *think* pretty much any data type could become “literalizeable” with something like this. Oh, and there’s nothing particularly magical about regular expressions as far as this idea is concerned; they’re just usually the first thing that comes to mind when I think of pattern matching in a string.&nbsp;</div><div class=""><br class=""></div><div class="">I know this looks like a lot of code, but the scary-looking parts with the regex stuff only has to be written once for each “type” of literal… types that want to be expressible by such a literal just have to write an init function.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">While I was writing this up, it occurred to me that another solution would be to have a set of "ExpressibleByValidated*Literal” protocols, where the init is failable and has to be @pure/@constexpr/@whateverthecompilerneedstorunitatcompiletime. That way the literal can be validated simply by calling the init and checking if it returns nil. Make `URL` conform to `ExpressibleByValidatedStringLiteral`, and you'll get the complie-time validation functionality just by copying whatever’s in the current `URL.init?(string: String)` function.</div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class="">- Dave Sweeris</div></div></div></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""></div></div></div></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>