<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 Dec 17, 2016, at 1:12 PM, Micah Hainline 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=""></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. </div></div></div></blockquote><br class=""></div><div>Sorry this took so long… the weekend kinda got away from me.</div><div><br class=""></div><div>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><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> += (</span><font face="Menlo" class="">name: </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: </font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">, </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: </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><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><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><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; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> url: </span><span style="font-variant-ligatures: no-common-ligatures" class="">StringLiteralType</span></div></div></div><div><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 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. </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><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: <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=""> </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><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=""> </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><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 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><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> += (</span><font face="Menlo" class="">name: </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=""> components: (</font><font face="Menlo" class="">(name: </font><font color="#d12f1b" face="Menlo" class="">“`protocol`"</font><font face="Menlo" class="">, type: </font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">, </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 face="Menlo" class="">),</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""> (name: </font><span style="color: rgb(187, 44, 162); font-family: Menlo;" class="">_</span><font face="Menlo" class="">, type: </font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">, </font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">pattern: </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=""> (name: </font><span style="color: rgb(209, 47, 27); font-family: Menlo;" class="">“domain"</span><font face="Menlo" class="">, </font><font face="Menlo" class="">type: </font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">, </font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">pattern: </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=""> (name: </font><font color="#d12f1b" face="Menlo" class="">“path”</font><font face="Menlo" class="">, </font><font face="Menlo" class="">type: </font><span style="color: rgb(112, 61, 170); font-family: Menlo;" class="">StringLiteralType</span><font face="Menlo" class="">, </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=""> protocol: </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><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><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=""> URLLiteralType: {</span></div></div></div><div><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; color: rgb(0, 0, 0);" class=""> </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; color: rgb(0, 0, 0);" class=""> `protocol`: </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; color: rgb(0, 0, 0);" class=""> </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; color: rgb(0, 0, 0);" class=""> domain: </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=""> let</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> path: </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><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: <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=""> </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=""> </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=""> </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=""> 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=""> </span><font color="#008400" face="Menlo" class="">//Do whatever with the components</font></div></div></div><div><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><div style="margin: 0px; line-height: normal;" class=""><div><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 </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. </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><div style="margin: 0px; line-height: normal;" class=""><div><div style="margin: 0px; line-height: normal;" class=""></div></div></div></div></body></html>