<div dir="ltr">On Mon, Dec 19, 2016 at 1:18 PM, Tony Allevato <span dir="ltr">&lt;<a href="mailto:allevato@google.com" target="_blank">allevato@google.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">+1 to the sentiment in your last paragraph.<br><div><br></div><div>In general, I&#39;m not a very big fan of the #foo(...) syntax for literals and I think using that as the starting point for discussion biases us towards those when more general and powerful alternatives could exist. That syntax exists for Playground support and I&#39;d hate to see it proliferate into non-playground sources. Furthermore, if the user could write a compile-time-validating initializer for `URL(&quot;<a href="http://foo.com/bar" target="_blank">http://foo.com/bar</a>&quot;)` (or `let url: URL = &quot;<a href="http://foo.com/bar" target="_blank">http://foo.com/bar</a>&quot;`), then that expression becomes just as much a &quot;literal&quot; as something like `#url(&quot;<a href="http://foo.com/bar" target="_blank">http://foo.com/bar</a>&quot;)`. There&#39;s no functional difference between the two: both are sequences of tokens that the compiler can evaluate statically. The benefit is that the end user doesn&#39;t have to worry about the distinction; the compiler picks the appropriate evaluation automatically depending on whether the initializer argument is statically known or not.</div></div></blockquote><div><br></div><div>+1 to this. If we could mark an initializer for URL as a constexpr that takes a string literal, then conformance to ExpressibleByStringLiteral would make `let foo = URL(&quot;<a href="http://example.com">http://example.com</a>&quot;)` work exactly as desired at compile time, and this is IMO the most elegant solution.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Taking this a step further, if we can get constexpr-like validation for initializers like this, then I wonder if the #foo literal syntax needs to exist at all, and I&#39;d propose that we abandon it and just let the playground UI and other IDEs transform things like `UIColor(red: 0.5, green: 0.5, blue: 0.5)` into a swatch when it sees them if they can be evaluated at compile-time. What you lose is, as Erica mentioned earlier, the fact that `#colorLiteral(...)` is untyped and can be turned into multiple color types, but the value already has to be assigned to a concrete type eventually anyway.</div><div><br></div><div>Am I missing any use cases for things like #colorLiteral that doing this would make difficult or impossible?</div><div><br></div></div><br><div class="gmail_quote"><div><div class="h5"><div dir="ltr">On Mon, Dec 19, 2016 at 10:53 AM David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br></div></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div style="word-wrap:break-word" class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><blockquote type="cite" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg">On Dec 19, 2016, at 1:26 AM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="m_5990304029815748969gmail_msg" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="m_5990304029815748969m_1632862851570904145Apple-interchange-newline m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div dir="ltr" class="m_5990304029815748969gmail_msg">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="m_5990304029815748969gmail_msg" target="_blank">https://webkit.org/blog/7086/<wbr>url-parsing-in-webkit/</a>&gt;. The full spec is here: &lt;<a href="https://url.spec.whatwg.org/" class="m_5990304029815748969gmail_msg" target="_blank">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&#39;d expect it to be the full spec, as we do with Unicode.<div class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"><div class="gmail_extra m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"><div class="gmail_quote m_5990304029815748969gmail_msg">On Mon, Dec 19, 2016 at 2:26 AM, Benjamin Spratling via swift-evolution <span dir="ltr" class="m_5990304029815748969gmail_msg">&lt;<a href="mailto:swift-evolution@swift.org" class="m_5990304029815748969gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br class="m_5990304029815748969gmail_msg"><blockquote class="gmail_quote m_5990304029815748969gmail_msg" 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 style="word-wrap:break-word" class="m_5990304029815748969gmail_msg">Howdy,<div class="m_5990304029815748969gmail_msg"><span class="m_5990304029815748969m_1632862851570904145gmail-m_-1448418347467680467Apple-tab-span m_5990304029815748969gmail_msg" style="white-space:pre-wrap">        </span>Yes, I was also intrigued by the “Regex” validation mentioned in another post.  It could offer a convenient way to get some literals support in without the headaches associated with the constexpr C++ approach.</div><div class="m_5990304029815748969gmail_msg"><span class="m_5990304029815748969m_1632862851570904145gmail-m_-1448418347467680467Apple-tab-span m_5990304029815748969gmail_msg" style="white-space:pre-wrap">        </span>I’m curious, though, how many types can we image in can be validated by this method?  If it really is just URL’s, then I’d actually lean towards making this a compiler magic feature.</div><div class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg"><span class="m_5990304029815748969m_1632862851570904145gmail-m_-1448418347467680467Apple-tab-span m_5990304029815748969gmail_msg" style="white-space:pre-wrap">        </span>Someone else mentioned fetching the URL’s for a preview.  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.  At least we shouldn’t for non-file-scheme URLs.  IMHO, verifying that a service is active isn’t really the Swift compiler’s job.  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="m_5990304029815748969gmail_msg">-Ben</div><div class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><blockquote type="cite" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969m_1632862851570904145gmail-h5 m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg">On Dec 19, 2016, at 1:41 AM, David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="m_5990304029815748969gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_5990304029815748969m_1632862851570904145gmail-m_-1448418347467680467Apple-interchange-newline m_5990304029815748969gmail_msg"></div></div><div class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969m_1632862851570904145gmail-h5 m_5990304029815748969gmail_msg"><div style="word-wrap:break-word" class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><blockquote type="cite" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg">On Dec 17, 2016, at 1:12 PM, Micah Hainline via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="m_5990304029815748969gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_5990304029815748969m_1632862851570904145gmail-m_-1448418347467680467Apple-interchange-newline m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div dir="auto" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg">I&#39;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="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg">Sorry this took so long… the weekend kinda got away from me.</div><div class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg">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:0px 0px 0px 40px;border:none;padding:0px" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"><font color="#bb2ca2" class="m_5990304029815748969gmail_msg">#literalpatterns</font> += (</span><font face="Menlo" class="m_5990304029815748969gmail_msg">name: </font><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">“URLLiteralType</span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">”</font><font face="Menlo" class="m_5990304029815748969gmail_msg">, components</font><font face="Menlo" class="m_5990304029815748969gmail_msg">: (name: url, type: </font><span style="color:rgb(112,61,170);font-family:menlo" class="m_5990304029815748969gmail_msg">StringLiteralType</span><font face="Menlo" class="m_5990304029815748969gmail_msg">, </font><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">patte<wbr>rn: </span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">“(http|https)://</font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">(www.)?[a-z|A-<wbr>Z|0-9]+</font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">.(com|org|net)(/</font><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">[a-z|A-<wbr>Z|0-9]+)*(/</span><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">[a-z|A-Z|0-9]+.</span><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">[a-<wbr>z|A-Z|0-9]+)?</span><font face="Menlo" class="m_5990304029815748969gmail_msg"><font color="#d12f1b" class="m_5990304029815748969gmail_msg">”</font>), </font><font face="Menlo" class="m_5990304029815748969gmail_msg">protocol: </font><span style="color:rgb(79,129,135);font-family:menlo" class="m_5990304029815748969gmail_msg">ExpressibleByURLLite<wbr>ral</span><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">)</span></div></div></blockquote><div class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg">This would let the compiler know pretty much everything it needs to know… that the “new” type is called “URLLiteralType&quot;, 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&quot; the wrong terminology… pardon my ignorance)</div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">struct</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> URLLiteralType: {</span></div></div></div></div><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo;color:rgb(112,61,170)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> url: </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">StringLiteralType</span></div></div></div><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">}</span></div></div></div></blockquote><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">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:0px 0px 0px 40px;border:none;padding:0px" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">init</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">(urlLiteral value: <span style="color:rgb(79,129,135)" class="m_5990304029815748969gmail_msg">URLLiteralType</span>) {</span></div><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> urlString = value.url</span></div></div></div><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><font color="#008400" face="Menlo" class="m_5990304029815748969gmail_msg">//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="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">}</span></div></div></div></blockquote><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></div></div><div class="m_5990304029815748969gmail_msg">If it’d be more useful, the pattern could be split into multiple pieces:</div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"><font color="#bb2ca2" class="m_5990304029815748969gmail_msg">#literalpatterns</font> += (</span><font face="Menlo" class="m_5990304029815748969gmail_msg">name: </font><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">“URLLiteralType</span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">”</font><font face="Menlo" class="m_5990304029815748969gmail_msg">,</font></div><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><font face="Menlo" class="m_5990304029815748969gmail_msg">                     components: (</font><font face="Menlo" class="m_5990304029815748969gmail_msg">(name: </font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">“`protocol`&quot;</font><font face="Menlo" class="m_5990304029815748969gmail_msg">, type: </font><span style="color:rgb(112,61,170);font-family:menlo" class="m_5990304029815748969gmail_msg">StringLiteralType</span><font face="Menlo" class="m_5990304029815748969gmail_msg">, </font><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">patte<wbr>rn: </span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">“(http|https)”</font><font face="Menlo" class="m_5990304029815748969gmail_msg">),</font></div><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><font face="Menlo" class="m_5990304029815748969gmail_msg">                                  (name: </font><span style="color:rgb(187,44,162);font-family:menlo" class="m_5990304029815748969gmail_msg">_</span><font face="Menlo" class="m_5990304029815748969gmail_msg">,            type: </font><span style="color:rgb(112,61,170);font-family:menlo" class="m_5990304029815748969gmail_msg">StringLiteralType</span><font face="Menlo" class="m_5990304029815748969gmail_msg">, </font><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">patt<wbr>ern: </span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">“://”</font><font face="Menlo" class="m_5990304029815748969gmail_msg">),</font></div><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><font face="Menlo" class="m_5990304029815748969gmail_msg">                                  (name: </font><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">“domain&quot;</span><font face="Menlo" class="m_5990304029815748969gmail_msg">,     </font><font face="Menlo" class="m_5990304029815748969gmail_msg">type: </font><span style="color:rgb(112,61,170);font-family:menlo" class="m_5990304029815748969gmail_msg">StringLiteralType</span><font face="Menlo" class="m_5990304029815748969gmail_msg">, </font><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">pat<wbr>tern: </span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">“(www.)?[a-z|A-Z|0-9]+</font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">.(<wbr>com|org|net)</font><font face="Menlo" class="m_5990304029815748969gmail_msg"><font color="#d12f1b" class="m_5990304029815748969gmail_msg">”</font>),</font></div><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><font face="Menlo" class="m_5990304029815748969gmail_msg">                                  (name: </font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">“path”</font><font face="Menlo" class="m_5990304029815748969gmail_msg">,       </font><font face="Menlo" class="m_5990304029815748969gmail_msg">type: </font><span style="color:rgb(112,61,170);font-family:menlo" class="m_5990304029815748969gmail_msg">StringLiteralType</span><font face="Menlo" class="m_5990304029815748969gmail_msg">, </font><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">pat<wbr>tern: </span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">&quot;(/</font><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">[a-z|A-Z|0-9]+)*(/</span><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">[a-z|A-Z|<wbr>0-9]+.</span><span style="color:rgb(209,47,27);font-family:menlo" class="m_5990304029815748969gmail_msg">[a-z|A-Z|0-9]+)?</span><font face="Menlo" class="m_5990304029815748969gmail_msg"><font color="#d12f1b" class="m_5990304029815748969gmail_msg">”</font>))</font></div><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><font face="Menlo" class="m_5990304029815748969gmail_msg">                     protocol: </font><span style="color:rgb(79,129,135);font-family:menlo" class="m_5990304029815748969gmail_msg">ExpressibleByURLLit<wbr>eral</span><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">)</span></div></div></blockquote><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg">This would result in URLLiteralType looking like this:</div><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">struct</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> URLLiteralType: {</span></div></div></div><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo;color:rgb(112,61,170)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> `protocol`: </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">StringLitera<wbr>lType</span></div><div style="margin:0px;line-height:normal;font-family:menlo;color:rgb(112,61,170)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> domain: </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">StringLiteralTyp<wbr>e</span></div><div style="margin:0px;line-height:normal;font-family:menlo;color:rgb(112,61,170)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">    let</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> path: </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">StringLiteralType</span></span></div><div style="margin:0px;line-height:normal;font-family:menlo;color:rgb(112,61,170)" class="m_5990304029815748969gmail_msg">}</div></div></div></blockquote>And in the init would start out like this:</div><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">init</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">(urlLiteral value: <span style="color:rgb(79,129,135)" class="m_5990304029815748969gmail_msg">URLLiteralType</span>) {</span></div><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> protocolType = value.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="m_5990304029815748969gmail_msg">protocol</span></div><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> domain = value.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="m_5990304029815748969gmail_msg">domain</span></div><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="m_5990304029815748969gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> path = value.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="m_5990304029815748969gmail_msg">path</span></div><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><font color="#008400" face="Menlo" class="m_5990304029815748969gmail_msg">//Do whatever with the components</font></div></div></div><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;font-family:menlo" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">}</span></div></div></div></blockquote></div></div><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></span></div><div class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">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="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg">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="m_5990304029815748969gmail_msg"></div></div><div style="word-wrap:break-word" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg">It doesn’t have to be regex per se… instead of</div></div><div style="word-wrap:break-word" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"><font color="#bb2ca2" class="m_5990304029815748969gmail_msg">#literalpatterns</font> += (</span><font face="Menlo" class="m_5990304029815748969gmail_msg">name: </font><span style="color:rgb(209,47,27);font-family:Menlo" class="m_5990304029815748969gmail_msg">“URLLiteralType</span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">”</font><font face="Menlo" class="m_5990304029815748969gmail_msg">, components</font><font face="Menlo" class="m_5990304029815748969gmail_msg">: (name: url, type: </font><span style="color:rgb(112,61,170);font-family:Menlo" class="m_5990304029815748969gmail_msg">StringLiteralType</span><font face="Menlo" class="m_5990304029815748969gmail_msg">, </font><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">patte<wbr>rn: </span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">“(http|https)://</font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">(www.)?[a-<wbr>z|A-Z|0-9]+</font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">.(com|org|net)(/</font><span style="color:rgb(209,47,27);font-family:Menlo" class="m_5990304029815748969gmail_msg">[a-<wbr>z|A-Z|0-9]+)*(/</span><span style="color:rgb(209,47,27);font-family:Menlo" class="m_5990304029815748969gmail_msg">[a-z|A-Z|0-9]+.</span><span style="color:rgb(209,47,27);font-family:Menlo" class="m_5990304029815748969gmail_msg"><wbr>[a-z|A-Z|0-9]+)?</span><font face="Menlo" class="m_5990304029815748969gmail_msg"><font color="#d12f1b" class="m_5990304029815748969gmail_msg">”</font>), </font><font face="Menlo" class="m_5990304029815748969gmail_msg">protocol: </font><span style="color:rgb(79,129,135);font-family:Menlo" class="m_5990304029815748969gmail_msg"><wbr>ExpressibleByURLLiteral</span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">)</span></div></div><div style="word-wrap:break-word" class="m_5990304029815748969gmail_msg"><div class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">I probably should’ve written something more like:</span></div><div class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"><font color="#bb2ca2" class="m_5990304029815748969gmail_msg">#literalpatterns</font> += (</span><font face="Menlo" class="m_5990304029815748969gmail_msg">name: </font><span style="color:rgb(209,47,27);font-family:Menlo" class="m_5990304029815748969gmail_msg">“URLLiteralType</span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">”</font><font face="Menlo" class="m_5990304029815748969gmail_msg">, components</font><font face="Menlo" class="m_5990304029815748969gmail_msg">: (name: url, type: </font><span style="color:rgb(112,61,170);font-family:Menlo" class="m_5990304029815748969gmail_msg">StringLiteralType</span><font face="Menlo" class="m_5990304029815748969gmail_msg">, </font><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">match<wbr>ing: Regex(</span><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">“(http|https)://</font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">(<wbr>www.)?[a-z|A-Z|0-9]+</font><font color="#d12f1b" face="Menlo" class="m_5990304029815748969gmail_msg">.(com|org|<wbr>net)(/</font><span style="color:rgb(209,47,27);font-family:Menlo" class="m_5990304029815748969gmail_msg">[a-z|A-Z|0-9]+)*(/</span><span style="color:rgb(209,47,27);font-family:Menlo" class="m_5990304029815748969gmail_msg">[a-z|<wbr>A-Z|0-9]+.</span><span style="color:rgb(209,47,27);font-family:Menlo" class="m_5990304029815748969gmail_msg">[a-z|A-Z|0-9]+)?</span><font face="Menlo" class="m_5990304029815748969gmail_msg"><font color="#d12f1b" class="m_5990304029815748969gmail_msg">”</font>)),<wbr> </font><font face="Menlo" class="m_5990304029815748969gmail_msg">protocol: </font><span style="color:rgb(79,129,135);font-family:Menlo" class="m_5990304029815748969gmail_msg">ExpressibleByURLLit<wbr>eral</span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">)</span></span></div>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&quot; 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.<div class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg">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 <i class="m_5990304029815748969gmail_msg">should</i> be kinda scary and hard to understand because it’s most certainly not an area beginners should be in. The second mechanism <span style="font-size:11px" class="m_5990304029815748969gmail_msg">— </span>something like that `<wbr>ExpressibleByValidatedStringLi<wbr>teral` idea — 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="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><div style="font-family:Menlo;margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="m_5990304029815748969gmail_msg">protocol</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"> ExpressibleByValidatedStringLi<wbr>teral {</span></div><div style="font-family:Menlo;margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="m_5990304029815748969gmail_msg">init</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">?(stringLiteral value: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="m_5990304029815748969gmail_msg">StringLiteralType</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">)</span></div><div style="font-family:Menlo;margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">}</span></div><div style="font-family:Menlo;margin:0px;line-height:normal;color:rgb(79,129,135)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="m_5990304029815748969gmail_msg">struct</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg"> URL : </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">ExpressibleByValidatedStringLi<wbr>teral</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg"> {</span></div><div style="font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">//Stuff</span></div><div style="font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">//...</span></div><div style="font-family:Menlo;margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="m_5990304029815748969gmail_msg">init</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">?(stringLiteral value: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="m_5990304029815748969gmail_msg">StringLiteralType</span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">) {</span></div><div style="font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg">        </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">//Perform validation here; return nil if it fails</span></div><div style="font-family:Menlo;margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">    }</span></div><div style="font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">//...</span></div><div style="font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">//Things</span></div><div style="font-family:Menlo;margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">}</span></div><div style="font-family:Menlo" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal;color:rgb(0,132,0)" class="m_5990304029815748969gmail_msg"><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="m_5990304029815748969gmail_msg">var</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg"> lru: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="m_5990304029815748969gmail_msg">URL</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b" class="m_5990304029815748969gmail_msg">&quot;foo&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">//Compiler throws this to the init? function, it returns nil, the compiler raises a syntax error</span></div><div style="margin:0px;line-height:normal" class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="m_5990304029815748969gmail_msg">var</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg"> url: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="m_5990304029815748969gmail_msg">URL</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b" class="m_5990304029815748969gmail_msg">&quot;<a href="http://www.some.valid.url.com" class="m_5990304029815748969gmail_msg" target="_blank">http://www.some.valid.url.com</a><wbr>&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="m_5990304029815748969gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg">//Compiler throws this to the init? function, it returns an optional URL, the compiler unwraps it and does the assignment</span></div><div class="m_5990304029815748969gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></span></div></div></div><div class="m_5990304029815748969gmail_msg">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="m_5990304029815748969gmail_msg"><br class="m_5990304029815748969gmail_msg"></div><div class="m_5990304029815748969gmail_msg">- Dave Sweeris</div></div></div></div></div></div><span class="">______________________________<wbr>_________________<br class="m_5990304029815748969gmail_msg">
swift-evolution mailing list<br class="m_5990304029815748969gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="m_5990304029815748969gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="m_5990304029815748969gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="m_5990304029815748969gmail_msg" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br class="m_5990304029815748969gmail_msg">
</span></blockquote></div>
</blockquote></div><br></div></div>