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

Kevin Ballard kevin at sb.org
Mon Aug 29 22:55:36 CDT 2016


On Mon, Aug 29, 2016, at 03:26 AM, Patrick Smith 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.

I find that much less readable.

> 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

Yes I did. I have a different check for == 1.0 where I check the Connection header. The full expression looks like (from the Swift 2.2 version):

  if response.headers["Connection"]?.caseInsensitiveCompare("close") == .OrderedSame
      || (request?.httpVersion == HTTPVersion(1,0) && response.headers["Connection"]?.caseInsensitiveCompare("keep-alive") != .OrderedSame)
      || request?.httpVersion < HTTPVersion(1,0)
  {

-Kevin

> 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.
> 


More information about the swift-evolution mailing list