[swift-evolution] URL Literals

David Sweeris davesweeris at mac.com
Sat Dec 17 09:56:34 CST 2016



Sent from my iPhone

> On Dec 16, 2016, at 12:46, Micah Hainline via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I would like to be able to create a URL literal that is compile-time
> checked for correct format. This would help avoid code like this:
> 
>    let url: URL = URL(string: "https://example.com")!
> 
> The cleanest way I can think of doing that would be to introduce a new
> macro structure similar to #selector, though I'm open to other ideas.
> I would think it should take a quoted literal string to avoid
> problems. That would look like this:
> 
>    let url: URL = #url("https://example.com")
> 
> What does everyone think of that idea?

I'd rather see the literal validation happen without special call-site syntax, like this:
    let url: URL = "https://example.com"

And move all the pseudo-magic to a user-definable "ExpressibleByURLLiteral" (in this case) protocol, which itself contains the validation mechanism, be it regex, compiler functions, pure functions, or something else.

- Dave Sweeris 


More information about the swift-evolution mailing list