[swift-server-dev] Convert HTTPVersion to struct

Rien Rien at Balancingrock.nl
Wed Jun 14 04:36:04 CDT 2017


> On 14 Jun 2017, at 11:14, Chris Bailey <BAILEYC at uk.ibm.com> wrote:
> 
> One factor is whether you consider the version to be discrete values or a structured format for data - I'd argue that we have a structured format due to the expectation that future versions of the HTTP spec could arrive. 

But do you want your code littered with numerical tests or conceptual tests?
Suppose a new version of HTTP does arrive, do you really want to manually walk through the code and find all tests on the numerical values?
Or would it be easier to use switch statements on an enum and have all missing cases pointed out to you?

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift

> 
> Adding conformance to Hashable/Equatable and CustomStringConvertible provides the operations, and whilst it may look strange we could also provide convenience constants if it makes sense, ie: 
>     public static let http1_0 = HTTPVersion(major: 1, minor: 0) 
>     public static let http1_1 = HTTPVersion(major: 1, minor: 1) 
>     public static let http2_0 = HTTPVersion(major: 2, minor: 0)
> 
> Having said that, there is slight additional storage overhead for structs vs. enums, as a slight additional compare cost. 
> 
> Chris 
> 
> Michael Chiu <hatsuneyuji at icloud.com> wrote on 14/06/2017 08:12:35:
> 
> > From: Michael Chiu <hatsuneyuji at icloud.com> 
> > To: Rien <Rien at Balancingrock.nl> 
> > Cc: Chris Bailey <BAILEYC at uk.ibm.com>, swift-server-dev at swift.org 
> > Date: 14/06/2017 08:12 
> > Subject: Re: [swift-server-dev] Convert HTTPVersion to struct 
> > 
> > I think currently the http version is come from the http_parser 
> > which has parsed the version string to number anyway. hence the 
> > performance should be similar.
> > 
> > I do see some advantage to use struct tho, for example comparing 
> > only major version.
> > 
> > Michael
> > 
> > > On Jun 13, 2017, at 11:41 PM, Rien via swift-server-dev <swift-
> > server-dev at swift.org> wrote:
> > > 
> > > The choices seem to be:
> > > 
> > > 1) String
> > > 2) Enum
> > > 3) Tuple
> > > 4) Struct
> > > 5) Class
> > > 
> > > 1) String
> > > Advantage: it is in fact an utf-8 string. No conversions needed.
> > > Disadvantage: It is mostly thought of as a number, thus the 
> > abstraction does not match the type, very minor performance penalty 
> > in comparing. Illogical to add HTTP-version extensions to the type.
> > > 
> > > 2) Enum
> > > Advantage: shortest possible representation (?), fastest compare 
> > (?), easy to use.
> > > Disadvantage: Needs converting to and from.
> > > 
> > > 3) Tuple
> > > Advantage: Matches fairly wel with the abstraction.
> > > Disadvantage: Fairly complex conversion, adding members/operations
> > is non-intuitive, not very easy to use.
> > > 
> > > 4) Struct
> > > Advantage: Matches fairly wel with the abstraction.
> > > Disadvantage: Fairly complex conversion. Not very easy to use 
> > without adding members/operations.
> > > 
> > > 5) This was for completeness only, I would be very surprised if 
> > anybody would actually propose this… ;-)
> > > 
> > > 
> > > To me that leaves option 2 and 4.
> > > Advantage of 2 over 4:
> > > 
> > > - I find the abstraction a better match. The difference between 1,
> > 1.1 and 2 is such that it makes little sense to be to differentiate 
> > based on the numerical values. It makes much more sense to 
> > differentiate based on the “whole number”. AFAIIC they could just as
> > easily have named the versions A, B, C etc. The numbers do not 
> > convey any information as such.
> > > - Switch is easier and more intuitive to use.
> > > - Enums can have additional properties and operations similar to struct.
> > > 
> > > Advantage of 4 over 2:
> > > I don’t see any.
> > > 
> > > Regards,
> > > Rien
> > > 
> > > Site: http://balancingrock.nl
> > > Blog: http://swiftrien.blogspot.com
> > > Github: http://github.com/Balancingrock
> > > Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift
> > > 
> > > 
> > > 
> > >> On 13 Jun 2017, at 22:44, Chris Bailey via swift-server-dev 
> > <swift-server-dev at swift.org> wrote:
> > >> 
> > >> Based on our discussed approach of reviewing each of the types in
> > the HTTP project, I've raised the following PR to convert 
> > HTTPVersion from (Int, Int) to a struct based on Paulo's proposal: 
> > >>       https://github.com/swift-server/http/pull/6 
> > >> 
> > >> I've not yet added Hashable, Equatable and 
> > CustomStringConvertible protocols - we can do that if we're happy 
> > with the change. 
> > >> 
> > >> Chris
> > >> Unless stated otherwise above:
> > >> IBM United Kingdom Limited - Registered in England and Wales with
> > number 741598. 
> > >> Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU
> > >> _______________________________________________
> > >> swift-server-dev mailing list
> > >> swift-server-dev at swift.org
> > >> https://lists.swift.org/mailman/listinfo/swift-server-dev
> > > 
> > > _______________________________________________
> > > swift-server-dev mailing list
> > > swift-server-dev at swift.org
> > > https://lists.swift.org/mailman/listinfo/swift-server-dev
> > 
> 
> Unless stated otherwise above:
> IBM United Kingdom Limited - Registered in England and Wales with number 741598. 
> Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU



More information about the swift-server-dev mailing list