apple / swift-nio

Event-driven network application framework for high performance protocol servers & clients, non-blocking.
https://swiftpackageindex.com/apple/swift-nio/documentation
Apache License 2.0
8k stars 652 forks source link

[Enhancement]Add an initial dummy value to HTTPResponseStatus #224

Closed pushkarnk closed 6 years ago

pushkarnk commented 6 years ago

I have been working with SwiftNIO over the last couple of days. In the previous networking framework that we used, there was support to initialise vars tracking the HTTP status code with a dummy value like unknown = -1.

The only way to do something similar with HTTPResponseStatus is by using the custom value. However, IMO, simple checks tend to become bigger because .custom has associated values.

var statusCode: HTTPResponseStatus = .custom(code: 0, reasonPhrase: "Unknown")
...
if case .custom(let value, _) = x, value == 0 { 
}

If a dummy value were added to the HTTPResponseStatus enum, things may be easier to express!

var statusCode: HTTPResponseStatus = .unknown
...
if statusCode == .unknown {
}

If there are alternate ways to achieve this without an addition to the enum, I'll be happy to know what they are!

helje5 commented 6 years ago

The way you would do this in Swift is by using an Optional, no?

pushkarnk commented 6 years ago

Yes, I agree that using an Optional is another way. But won't that increase the number of checks?

Lukasa commented 6 years ago

No. In fact, using an Optional enum is exactly isomorphic to what you've asked for: the Swift compiler will synthesize an extra enum case to be the "not present" value in any situation where you're using the Optional.

pushkarnk commented 6 years ago

Thanks for the suggestion!