> On Nov 24, 2017, at 4:15 PM, Chris Lattner <clatt...@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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to