[swift-evolution] [Pitch] Raw mode string literals

Chris Lattner clattner at nondot.org
Fri Nov 24 18:25:02 CST 2017



> On Nov 24, 2017, at 4:15 PM, Chris Lattner <clattner at nondot.org> wrote:
> 
>> 
>> than the same type having a collection of named matches using the usual Perl syntax?
>> 
>>   if case /(?<firstName>[a-zA-Z]+) (?<lastName>[a-zA-Z]+)/ = getSomeString() {
>>     print(Regex.captured["firstName"], Regex.captured["lastName"])
>>   }
> 
> Personally, I really don’t like this.  It turns a structured problem into one that violates DRY and loses the structure inherent in the solution.  Also, while theoretically the dictionary could be optimized away, in practice that would be difficult to do without heroics.
> 

One other minor and obscure point: if the compiler is aware of the regex grammar it can properly type the matches, I can imagine the following cases:

if case /(let name: [a-zA-Z]+) (let count: Int)/ = getSomeString() {
   print(name, count)
}

-> name has type String, count has type Int (and matches [0-9]+)


if case /(let name: [a-zA-Z]+)? (let count: Int)/ = getSomeString() {
   print(name, count)
}

-> name has type String?

if case /(let name: [a-zA-Z]+)* (let count: Int)/ = getSomeString() {
   print(name, count)
}

-> name has type [String]

etc.  Even if we don’t have a “default regex” for types, it would still be awesome to be able to write:


if case /(let name: [a-zA-Z]+) (let count: Int: [0-9]+)/ = getSomeString() {
   print(name, count)
}

and have that transparently invoke and check the Int?(string) failable initializer.

-Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171124/3fbd7cde/attachment.html>


More information about the swift-evolution mailing list