[swift-evolution] URL Literals

Derrick Ho wh1pch81n at gmail.com
Fri Dec 16 17:50:52 CST 2016


let url = URL(string: "https://example.com")!

let url = #url("https://example.com")

Are not that different in length. It really isn't saving you much.

I suppose you can try overloading an operator to get something to the
effect you want.
On Fri, Dec 16, 2016 at 6:19 PM Micah Hainline via swift-evolution <
swift-evolution at swift.org> wrote:

> Exactly! It's not an earth-shattering pain, but it would be nice to have
> clean type safety there.
>
> > On Dec 16, 2016, at 4:01 PM, Charlie Monroe <charlie at charliemonroe.net>
> wrote:
> >
> >
> >>> On Dec 16, 2016, at 10:05 PM, Charles Srstka via swift-evolution <
> swift-evolution at swift.org> wrote:
> >>>
> >>> On Dec 16, 2016, at 2:46 PM, 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?
> >>> _______________________________________________
> >>> swift-evolution mailing list
> >>> swift-evolution at swift.org
> >>> https://lists.swift.org/mailman/listinfo/swift-evolution
> >>
> >>
> >> I’d like to see something like that for file path URLs. For something
> so commonly used, URL(fileURLWithPath:) is obnoxiously verbose.
> >>
> >> Charles
> >
> > Yes, but it's not a nullable initializer. With URL(string:) the
> incredible pain is that even compile-time URLs to your own website are
> nullable URL? and you need to force-unwrap them.
> >
> >>
> >> _______________________________________________
> >> swift-evolution mailing list
> >> swift-evolution at swift.org
> >> https://lists.swift.org/mailman/listinfo/swift-evolution
> >
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161216/c80145f3/attachment.html>


More information about the swift-evolution mailing list