[swift-evolution] [Accepted] SE-0121: Remove Optional Comparison Operators

Sean Heber sean at fifthace.com
Mon Aug 29 09:42:11 CDT 2016


I agree - I really don’t like these clever map constructs to work around optionals either and I avoid them at all costs. IMO they obscure intent pretty severely and shouldn’t be considered idiomatic Swift.

Depending on circumstances, I might do something along these lines:

guard let version = request?.httpVersion, version >= HTTPVersion(1.0) else {
  disconnect()
  return
}

That’s still more verbose, though, but it seems very explicit without relying on map cleverness.

l8r
Sean


> On Aug 29, 2016, at 6:05 AM, Goffredo Marocchi via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Whenever I see such examples I feel like map is being abused because of some of its properties rather than this. Ring the best way to deal with optionals.
> 
> Sent from my iPhone
> 
> On 29 Aug 2016, at 11:26, Patrick Smith via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> A little nicer I think is:
>> 
>> if request?.httpVersion.map({ $0 < HTTPVersion(1.0) }) ?? true {
>> 
>> It’s very explicit what the fallback is too, the original’s ambiguity makes me uncomfortable.
>> 
>> BTW, did you want to be checking for <= 1.0? With HTTP 1.0, it’s opt in. https://en.wikipedia.org/wiki/HTTP_persistent_connection
>> 
>> Patrick
>> 
>>> On 28 Aug 2016, at 1:20 PM, Kevin Ballard via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> As for optional comparisons making the code cleaner, I end up using them all over the place. The case that motivated my email looked something along the lines of
>>> 
>>> if request?.httpVersion < HTTPVersion(1.0) {
>>>   // no keepalive
>>>   disconnect()
>>> }
>>> 
>>> This particular case could be trivially replaced with
>>> 
>>> if request.map({ $0.httpVersion < HTTPVersion(1.0) }) ?? true {
>>> 
>>> but it’s uglier and harder to read.
>> 
>> _______________________________________________
>> 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



More information about the swift-evolution mailing list