[swift-evolution] Strings in Swift 4

Ben Cohen ben_cohen at apple.com
Fri Jan 20 20:26:52 CST 2017


> On Jan 20, 2017, at 6:11 PM, Joe Groff <jgroff at apple.com> wrote:
> 
> 
>> On Jan 20, 2017, at 5:15 PM, Ben Cohen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> 
>>> On Jan 20, 2017, at 3:29 PM, Jaden Geller via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> Wouldn’t `x[…]` be more consistent with these other syntaxes?
>>> 
>> 
>> Maybe (though are those extra characters really telling you much?). 
>> 
>> But you can’t write that in Swift – you’d need a 0-argument operator.
>> 
>> (Or a […] postfix operator I guess if you wanted to try and sneak that through, but that is also not allowed…  :)
> 
> Technically, you can, since operators are function values:
> 
> struct Foo {}
> struct Woo { subscript(_: (Foo, Foo) -> Foo) -> Int { return 0 } }
> func ...(_ x: Foo, _ y: Foo) -> Foo { return x }
> 
> Woo()[...]
> 
> Whether you *want* to, though…
> 
> -Joe


I stand corrected, this totally works! Ship it...

extension String: Collection { }
extension Collection {
    subscript(_: (Self,Self)->Void) -> SubSequence {
        return self[startIndex..<endIndex]
    }
    static func ...(_ x: Self, _ y: Self) { fatalError() }
}


let s = "abcdef"
print(s[...])


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170120/7367e236/attachment.html>


More information about the swift-evolution mailing list