[swift-evolution] URL Literals
Jonathan Hull
jhull at gbis.com
Tue Dec 20 04:11:08 CST 2016
Yes, I agree. I am excited to see what happens in phase 2.
What I am suggesting here is slightly different. Basically being able to use RegEx (with capture groups) as a shorthand for a type composed of base literals. For example: (StringLiteral, [IntegerLiteral]). Named capture groups could even map to a dictionary literal. I am using “RegEx goes Here” to represent RegEx in the examples below, but hopefully it will get it’s own literal type in Xcode (Imagine that replacing it here).
func foo( _ param: “RegExGoesHere”) {…} //Definition uses a RegEx where the type would normally be
foo(“my parseable string") //Calling with a string literal
In this case, ‘param’ takes a string literal when called but the compiler converts it to a tuple of literals based on the regEx supplied and passes that tuple the function. The type/structure of the tuple is defined by the capture groups in the RegEx
The parameter above would only allow string literals to be passed in, and would give a compiler error if you tried to pass a variable or if the string didn’t conform to the supplied RegEx. To allow passing String variables you would have to add either ‘?’ or ‘!’ after the RegEx definition to handle the case where the value doesn’t conform.
func foo( _ param: “RegExGoesHere”?) {…} //‘param' is nil if RegEx fails
foo(myStringVar) //Calling
func bar( _ param: “RegExGoesHere”!) {…} //fatal error if RegEx fails
When a variable is passed, the RegEx is performed at runtime instead of compile time.
Once you have this, the syntax to add new literal types/initializers falls out virtually for free.
Thanks,
Jon
> On Dec 19, 2016, at 8:02 PM, Erica Sadun <erica at ericasadun.com> wrote:
>
> Regex is another thing that appears on many platforms and has a standard way to express it.
>
> -- E
>
>> On Dec 19, 2016, at 5:51 PM, Jonathan Hull <jhull at gbis.com <mailto:jhull at gbis.com>> wrote:
>>
>> +1 to Erica’s literal extensions (and Xiaodi’s idea of showing Favicons in Xcode where possible)
>>
>> Perhaps the easiest way to allow arbitrary literal extensions beyond those would be, in phase 2 when we add RegEx to the language, to take a RegEx defining the custom literal and have the compiler output a tuple of other literal types (including array literals for ‘*’, etc...) to the init method as a result of parsing it.
>>
>> It would actually be interesting to have the parsing via RegEx into literals as a general feature for parameters, and then the init syntax would fall out basically for free...
>>
>> Thanks,
>> Jon
>>
>>
>>> On Dec 18, 2016, at 2:17 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>
>>> I'd prefer to see a literal URL than a Foundation URL that is string-initializable. I don't see a URL literal as being in any way necessarily tightly coupled with a Foundation URL type. The point of a literal is that it is inherently typeless and checked at compile time. A color literal depending on context can be a UIColor or NSColor but that's not specified outside of the use context. The code is portable and cross platform.
>>>
>>> -- E
>>>
>>>
>>>> On Dec 17, 2016, at 10:18 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>
>>>> With respect to URL specifically, that it's a Foundation type may change the timeline as well. Various improvements to the Foundation API (and URL in particular) have been proposed here, but if I remember correctly, the stated goal was first to have a complete Swift version of Foundation, preserving the existing API as exactly as possible with no additions or subtractions, and only then to consider Swifty evolution of the APIs. I don't think the first step is complete yet.
>>>>
>>>> On Sat, Dec 17, 2016 at 21:46 Step C via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> Probably worth pointing out that this topic seems entirely additive. Which means it would be at least a phase 2 proposal, if not later.
>>>>
>>>> > On Dec 17, 2016, at 4:44 PM, Micah Hainline via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> >
>>>> > Yes, everyone who has what they feel like is a solid workable solution should write it up for URL and we can compare and pick holes in them all until we get something really solid.
>>>> >
>>>> >> On Dec 17, 2016, at 3:27 PM, David Sweeris <davesweeris at mac.com <mailto:davesweeris at mac.com>> wrote:
>>>> >>
>>>> >>
>>>> >>
>>>> >> Sent from my iPhone
>>>> >>
>>>> >>> On Dec 17, 2016, at 13:20, David Sweeris <davesweeris at mac.com <mailto:davesweeris at mac.com>> wrote:
>>>> >>>
>>>> >>>
>>>> >>>
>>>> >>> Sent from my iPhone
>>>> >>>
>>>> >>>> On Dec 17, 2016, at 13:12, Micah Hainline via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> >>>>
>>>> >>>> I'd love a fleshed out elegant example for URL that shows what a complete implementation of that special init method would look like.
>>>> >>>
>>>> >>> I can't do it now, but I'll try post one before tomorrow that shows how I'd envision it working.
>>>> >>
>>>> >> Oh, and to be clear, I'm not trying to "claim" this or anything... if anyone else has ideas, please post them! The more the merrier.
>>>
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161220/e8da836f/attachment.html>
More information about the swift-evolution
mailing list