<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=""><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">```swift</span></div><div class=""><span style="font-family: Menlo; font-size: 18px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">let</span><span style="font-family: Menlo; font-size: 18px; font-variant-ligatures: no-common-ligatures;" class=""> x = #imageLiteral(resourceName:"nothere.jpg")</span></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">print(x)</span></div></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">```</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">This compiles. It crashes at runtime. I don't see why URLs should be any different.</div><div class=""><br class=""></div><div class="">-- E</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 19, 2016, at 11:53 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=""><div style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Dec 19, 2016, at 1:26 AM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">URLs are unlikely to be something that can be validated by regex. See, for instance, this discussion: &lt;<a href="https://webkit.org/blog/7086/url-parsing-in-webkit/" class="">https://webkit.org/blog/7086/url-parsing-in-webkit/</a>&gt;. The full spec is here: &lt;<a href="https://url.spec.whatwg.org/" class="">https://url.spec.whatwg.org</a>&gt;. If Swift were to implement parsing of URLs at the level of the compiler or core library, I'd expect it to be the full spec, as we do with Unicode.<div class=""><br class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Dec 19, 2016 at 2:26 AM, Benjamin Spratling via swift-evolution<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span><span class="Apple-converted-space">&nbsp;</span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div class="" style="word-wrap: break-word;">Howdy,<div class=""><span class="gmail-m_-1448418347467680467Apple-tab-span" style="white-space: pre-wrap;">        </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="gmail-m_-1448418347467680467Apple-tab-span" style="white-space: pre-wrap;">        </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="gmail-m_-1448418347467680467Apple-tab-span" style="white-space: pre-wrap;">        </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 class=""><blockquote type="cite" class=""><div class=""><div class="gmail-h5"><div class="">On Dec 19, 2016, at 1:41 AM, David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="gmail-m_-1448418347467680467Apple-interchange-newline"></div></div><div class=""><div class=""><div class="gmail-h5"><div class="" style="word-wrap: break-word;"><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" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="gmail-m_-1448418347467680467Apple-interchange-newline"><div 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 class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;"><font color="#bb2ca2" class="">#literalpatterns</font>&nbsp;+= (</span><font face="Menlo" class="">name:&nbsp;</font><span class="" style="color: rgb(209, 47, 27); font-family: menlo;">“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 class="" style="color: rgb(112, 61, 170); font-family: menlo;">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;">patte<wbr class="">rn:<span class="Apple-converted-space">&nbsp;</span></span><font color="#d12f1b" face="Menlo" class="">“(http|https)://</font><font color="#d12f1b" face="Menlo" class="">(www.)?[a-z|A-<wbr class="">Z|0-9]+</font><font color="#d12f1b" face="Menlo" class="">.(com|org|net)(/</font><span class="" style="color: rgb(209, 47, 27); font-family: menlo;">[a-z|A-<wbr class="">Z|0-9]+)*(/</span><span class="" style="color: rgb(209, 47, 27); font-family: menlo;">[a-z|A-Z|0-9]+.</span><span class="" style="color: rgb(209, 47, 27); font-family: menlo;">[a-<wbr class="">z|A-Z|0-9]+)?</span><font face="Menlo" class=""><font color="#d12f1b" class="">”</font>),<span class="Apple-converted-space">&nbsp;</span></font><font face="Menlo" class="">protocol:&nbsp;</font><span class="" style="color: rgb(79, 129, 135); font-family: menlo;">ExpressibleByURLLite<wbr class="">ral</span><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;">)</span></div></div></blockquote><div class=""><div class=""><div class="" style="margin: 0px; line-height: normal;">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 class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">struct</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>URLLiteralType: {</span></div></div></div></div><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo; color: rgb(112, 61, 170);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>url:<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">StringLiteralType</span></div></div></div><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">}</span></div></div></div></blockquote><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;">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 class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">init</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(urlLiteral value:&nbsp;<span class="" style="color: rgb(79, 129, 135);">URLLiteralType</span>) {</span></div><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>urlString = value.url</span></div></div></div><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;">&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 class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">}</span></div></div></div></blockquote><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;"><br class=""></div></div><div class="">If it’d be more useful, the pattern could be split into multiple pieces:</div></div><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;"><font color="#bb2ca2" class="">#literalpatterns</font>&nbsp;+= (</span><font face="Menlo" class="">name:&nbsp;</font><span class="" style="color: rgb(209, 47, 27); font-family: menlo;">“URLLiteralType</span><font color="#d12f1b" face="Menlo" class="">”</font><font face="Menlo" class="">,</font></div><div class="" style="margin: 0px; line-height: normal;"><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 class="" style="color: rgb(112, 61, 170); font-family: menlo;">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;">patte<wbr class="">rn:&nbsp;</span><font color="#d12f1b" face="Menlo" class="">“(http|https)”</font><font face="Menlo" class="">),</font></div><div class="" style="margin: 0px; line-height: normal;"><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 class="" style="color: rgb(187, 44, 162); font-family: menlo;">_</span><font face="Menlo" class="">, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type:&nbsp;</font><span class="" style="color: rgb(112, 61, 170); font-family: menlo;">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;">patt<wbr class="">ern:&nbsp;</span><font color="#d12f1b" face="Menlo" class="">“://”</font><font face="Menlo" class="">),</font></div><div class="" style="margin: 0px; line-height: normal;"><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 class="" style="color: rgb(209, 47, 27); font-family: menlo;">“domain"</span><font face="Menlo" class="">, &nbsp; &nbsp;&nbsp;</font><font face="Menlo" class="">type:&nbsp;</font><span class="" style="color: rgb(112, 61, 170); font-family: menlo;">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;">pat<wbr class="">tern:&nbsp;</span><font color="#d12f1b" face="Menlo" class="">“(www.)?[a-z|A-Z|0-9]+</font><font color="#d12f1b" face="Menlo" class="">.(<wbr class="">com|org|net)</font><font face="Menlo" class=""><font color="#d12f1b" class="">”</font>),</font></div><div class="" style="margin: 0px; line-height: normal;"><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 class="" style="color: rgb(112, 61, 170); font-family: menlo;">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;">pat<wbr class="">tern:<span class="Apple-converted-space">&nbsp;</span></span><font color="#d12f1b" face="Menlo" class="">"(/</font><span class="" style="color: rgb(209, 47, 27); font-family: menlo;">[a-z|A-Z|0-9]+)*(/</span><span class="" style="color: rgb(209, 47, 27); font-family: menlo;">[a-z|A-Z|<wbr class="">0-9]+.</span><span class="" style="color: rgb(209, 47, 27); font-family: menlo;">[a-z|A-Z|0-9]+)?</span><font face="Menlo" class=""><font color="#d12f1b" class="">”</font>))</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;protocol:&nbsp;</font><span class="" style="color: rgb(79, 129, 135); font-family: menlo;">ExpressibleByURLLit<wbr class="">eral</span><span class="" style="font-family: menlo; font-variant-ligatures: no-common-ligatures;">)</span></div></div></blockquote><div class=""><div class="" style="margin: 0px; line-height: normal;">This would result in URLLiteralType looking like this:</div><div class="" style="margin: 0px; line-height: normal;"><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">struct</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;URLLiteralType: {</span></div></div></div><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo; color: rgb(112, 61, 170);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;`protocol`:&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures;">StringLitera<wbr class="">lType</span></div><div class="" style="margin: 0px; line-height: normal; font-family: menlo; color: rgb(112, 61, 170);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;domain:&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures;">StringLiteralTyp<wbr class="">e</span></div><div class="" style="margin: 0px; line-height: normal; font-family: menlo; color: rgb(112, 61, 170);"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">&nbsp; &nbsp; let</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;path:&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures;">StringLiteralType</span></span></div><div class="" style="margin: 0px; line-height: normal; font-family: menlo; color: rgb(112, 61, 170);">}</div></div></div></blockquote>And in the init would start out like this:</div><div class="" style="margin: 0px; line-height: normal;"><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">init</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(urlLiteral value:&nbsp;<span class="" style="color: rgb(79, 129, 135);">URLLiteralType</span>) {</span></div><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>protocolType = value.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">protocol</span></div><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>domain = value.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">domain</span></div><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;path = value.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">path</span></div><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><font color="#008400" face="Menlo" class="">//Do whatever with the components</font></div></div></div><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">}</span></div></div></div></blockquote></div></div><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;">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></div></div></div></div></div></div></div></blockquote></div></div></div></blockquote></div></div></div></div></div></blockquote><br class=""></div><div style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">It doesn’t have to be regex per se… instead of</div><div style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span class="" style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;"><font color="#bb2ca2" class="">#literalpatterns</font>&nbsp;+= (</span><font face="Menlo" class="">name:&nbsp;</font><span class="" style="color: rgb(209, 47, 27); font-family: Menlo;">“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 class="" style="color: rgb(112, 61, 170); font-family: Menlo;">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span class="" style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">pattern:&nbsp;</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 class="" style="color: rgb(209, 47, 27); font-family: Menlo;">[a-z|A-Z|0-9]+)*(/</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo;">[a-z|A-Z|0-9]+.</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo;">[a-z|A-Z|0-9]+)?</span><font face="Menlo" class=""><font color="#d12f1b" class="">”</font>),&nbsp;</font><font face="Menlo" class="">protocol:&nbsp;</font><span class="" style="color: rgb(79, 129, 135); font-family: Menlo;">ExpressibleByURLLiteral</span><span class="" style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">)</span></div><div style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span class="" style="font-variant-ligatures: no-common-ligatures;">I probably should’ve written something more like:</span></div><div style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="" style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;"><font color="#bb2ca2" class="">#literalpatterns</font>&nbsp;+= (</span><font face="Menlo" class="">name:&nbsp;</font><span class="" style="color: rgb(209, 47, 27); font-family: Menlo;">“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 class="" style="color: rgb(112, 61, 170); font-family: Menlo;">StringLiteralType</span><font face="Menlo" class="">,&nbsp;</font><span class="" style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">matching:&nbsp;Regex(</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 class="" style="color: rgb(209, 47, 27); font-family: Menlo;">[a-z|A-Z|0-9]+)*(/</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo;">[a-z|A-Z|0-9]+.</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo;">[a-z|A-Z|0-9]+)?</span><font face="Menlo" class=""><font color="#d12f1b" class="">”</font>)),&nbsp;</font><font face="Menlo" class="">protocol:&nbsp;</font><span class="" style="color: rgb(79, 129, 135); font-family: Menlo;">ExpressibleByURLLiteral</span><span class="" style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">)</span></span></div><span style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">where the `matching` argument can be anything that can (“@purely”-ly) use some specified mechanism (I’d vote for the ~= operator) with a literal to test whether it matches. Also, there is no existing `Regex` struct/class/mechanism in Swift, unless you count `NSRegularExpression`. I didn’t want to use that for a couple reasons… 1) I don’t think it’s part of the stdlib, and 2) it doesn’t have a non-failable init that just takes a string, so using it unmodified would kinda put us in a “it’s turtles all the way down” kind of situation. What I’d started doing was to look for the existing mechanism for specifying literals in the compiler so I could use the existing name for it (somehow I doubt there’s a actually an array of patterns called “literalpatterns" in the compiler) and copy the existing methods for specifying a valid literal. After being unsuccessful for some amount of time, I decided I was getting too tired and made up what I sent last night.</span><div class="" style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">The more I think about it, the more I’m starting to be of the opinion that we really ought to have two mechanisms here… One for specifying what constitutes a “base” literal (like `43`, `[“foo”, “bar”]`, or `true`), and one for types that merely need to perform some sort of validation on existing “base” literals. The first mechanism probably should be fairly arcane and involved, because you’d essentially be able to create new syntaxes, which<span class="Apple-converted-space">&nbsp;</span><i class="">should</i>&nbsp;be kinda scary and hard to understand because it’s most certainly not an area beginners should be in. The second mechanism&nbsp;<span class="" style="font-size: 11px;">—<span class="Apple-converted-space">&nbsp;</span></span>something like that `ExpressibleByValidatedStringLiteral` idea —&nbsp;isn’t nearly as complicated. In the case of URLs, I’d vote the second approach. We only really need two extra features to implement it (“@constexpr” and the compiler being able to use the REPL to evaluate @costexpr statements), and both of them have more uses other than just getting a few more compile-time checks or allowing for more inits to be non-failable. With both of those in place, getting an url “literal” becomes just this:</div><div class="" style="font-family: Palatino-Roman; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="" style="margin: 0px; line-height: normal;"><div class="" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">protocol</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>ExpressibleByValidatedStringLiteral {</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">init</span><span class="" style="font-variant-ligatures: no-common-ligatures;">?(stringLiteral value:<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">StringLiteralType</span><span class="" style="font-variant-ligatures: no-common-ligatures;">)</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">}</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal; color: rgb(79, 129, 135);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">struct</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;URL :<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">ExpressibleByValidatedStringLiteral</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>{</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//Stuff</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//...</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">init</span><span class="" style="font-variant-ligatures: no-common-ligatures;">?(stringLiteral value:<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">StringLiteralType</span><span class="" style="font-variant-ligatures: no-common-ligatures;">) {</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//Perform validation here; return nil if it fails</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp; }</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//...</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//Things</span></div><div class="" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;">}</span></div><div class="" style="font-family: Menlo;"><div class="" style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>lru:&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">URL</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;=<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"foo"</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//Compiler throws this to the init? function, it returns nil, the compiler raises a syntax error</span></div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>url:<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">URL</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;=<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"<a href="http://www.some.valid.url.com/" class="">http://www.some.valid.url.com</a>"</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//Compiler throws this to the init? function, it returns an optional URL, the compiler unwraps it and does the assignment</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div></div></div><div class="">I still very much want a way to define custom literals (precisely because it’d let me make new syntaxes), but I’m starting to think that something like the second, disappointingly easy idea, is probably the way to go in this case.</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div></div></div></div></blockquote></div><br class=""></body></html>