[swift-server-dev] [HTTP] Value vs Reference Types
Ben Cohen
ben_cohen at apple.com
Wed Dec 7 20:04:19 CST 2016
Hi Dan,
Thanks for spelling out these questions, I think they are a great starting point for a discussion. A few comments inline.
> On Nov 23, 2016, at 1:19 PM, Dan Appel via swift-server-dev <swift-server-dev at swift.org> wrote:
>
>
> My own responses:
>
>
> >1. Do we want to use concrete types or protocols for Request/Response?
>
>
> When working on Open Swift <https://github.com/open-swift>, this was a hot topic since we believed that it would be unsafe to have a protocol that would allow both value and reference types.
Bear in mind that a protocol is more than just the methods and types it declares – it is also its documentation. For example, a number of protocols in the standard library state things like complexity requirements in their documenting comments. The language has no way of enforcing these, but your type does not truly “conform” to the protocol unless you adhere to them. Value semantics are similar – you can document that it is invalid to implement a protocol with reference semantics. So I don’t think this is a blocker to wanting to use protocols.
> We arrived upon the `{Request|Response}Representable` pattern which worked but was a bit of a mess. Because of this, I would prefer concrete Request/Response types.
>
You could think of there as being 3 purposes to using protocols here, roughly in order of importance:
being able to write generic code
allowing different frameworks to interoperate
documenting what you need to implement
The first one is the only reason why a protocol must be included in a library, and the key question to ask when considering defining a protocol like this is “What common algorithms do you want to write across multiple different conforming types in your program”? (such as generic functions, including protocol extensions, or functions that take an existential if the protocol has no associated types).
This is distinct from wanting to be able to be able to choose from different library implementations of Request. You might want to choose between the Acme Inc Web Framework’s Request type, or some Swift-Server “official" Request type, but you never need to use both at once and write code spanning them. You just want to make sure that one can serve as a source-compatible “drop-in” replacement for the other in your code. This doesn’t mean you can’t write your own extensions – but you would extend the concrete Request not a RequestRepresentable protocol.
Next, it’s possible that there might be a collection of 3rd-party frameworks out there that don’t define Request, but want to be able to write methods that take or extend multiple possible Request implementations. This seems a bit unlikely in the case of these types, more likely in other cases like networking, so it’s kind of a what-if scenario where there are both multiple popular implementations of Request, and various frameworks that want to interact with them. Anyone can add a conformance to anything, so those frameworks can define a protocol of their own with a subset of the functionality they need, and then just extend the popular implementations to conform to it. If this gets really common, at that point it might be worth creating an official protocol for everyone to share – but this can be done later, doesn’t have to be done up-front.
Finally, if you do expect multiple implementations and want people to be able to swap them in and out when they choose, the protocol can serve to document what methods and properties you are expected to implement to be “source compatible". This can be done in documentation instead, the benefit of the protocol being it helps the library developer ensure they’ve got all the signatures right etc. But this isn’t something you expose to users, it’s something on the side to help implementors.
Based on all the above, it seems like there isn’t a pressing need for a protocol for these types right now and initial designs should focus on a concrete implementation until one emerges, if only to avoid premature generalization. Useful protocols tend to be discovered, rather than designed, through a desire to share common operations on different concrete types.
> >2. If we use concrete types, do we want value or reference semantics?
>
>
> What I think makes this easier is that the "big four" have each taken a slightly different approach that can be used as a reference.
> Zewo <https://github.com/Zewo/Zewo/blob/master/Modules/HTTP/Sources/HTTP/Message/Request.swift#L3> - struct, value semantics
> Vapor <https://github.com/vapor/engine/blob/master/Sources/HTTP/Models/Request/Request.swift#L4> - closed class, reference semantics
> Kitura <https://github.com/IBM-Swift/Kitura/blob/master/Sources/Kitura/RouterRequest.swift#L26> - closed class + has-a pattern, reference semantics
> Perfect <https://github.com/PerfectlySoft/Perfect-HTTP/blob/master/Sources/HTTPRequest.swift#L25> - class protocol, reference semantics
>
> Zewo is the outlier here, but I would like to note as a contributor to Zewo that we have not ran into situations where value semantics create an impassable roadblock.
>
> To me, it makes sense to pass them around as values since they don't have any logic of their own. Requests/Responses can't send themselves, they can only read and modified. It also gives me as a user more safety to pass them around since I know that they won't be modified implicitly.
>
> Take the following pseudo-code as an example:
>
> HTTPServer.onRequest { request in
> print(request.sourceIp)
> HTTPClient.send(request)
> print(request.sourceIp)
>
> }
>
> With reference semantics, there is no guarantee that sourceIp will be the same before and after sending off the request. After all, it could make sense for the HTTPClient to modify the sourceIp before sending off the request. This of course a contrived example, but the point stands.
>
Not contrived at all, this is a perfect illustration of why reference semantics make it harder to reason about your code and identify the cause of bugs.
> Anyway, I think it would be great if we could have people talk about their own experiences.
>
>
> >3. When is it more convenient to have reference semantics?
>
>
Convenience is a double-edged thing. Pointers with possibly-null values, or integer indexes into Unicode strings, are often considered convenient. But that convenience comes with a hidden cost to correctness – unexpected nulls, accidentally indexing into the middle of a grapheme cluster etc. When making a proper effort to handle these things correctly, code quickly becomes less convenient, and less readable, compared to the alternatives.
It’s generally the style in Swift that correctness shouldn't be sacrificed for convenience, but when things work out well, convenience and ergonomics can be mutually reinforcing – the code is nice to use correctly, awkward to use incorrectly. For example, optionals that force you to handle nil help with correctness, but they have sugar like ?? or optional chaining to handle common patterns clearly and idiomatically, ! as a shorthand for asserting something is non-nil etc.
> In the middleware chain architecture that we decided on in Zewo (the other ones have something similar), it can be convenient to modify requests in the responder and have that reflect in the middleware. I think this problem is best solved with `inout` parameters rather than reference types, but that is my personal opinion.
>
FWIW, this design view is also strongly held by those of us working on the Swift Standard Library. I also brought this up with several members of the Core Team and they also strongly felt that inout and value types was the general approach we should take with such types in Swift. The consensus there was that reference types really should be mostly used when identity of the value is important.
>
> >4. Are there problems that can't be solved with value semantics?
>
>
> I haven't found any, but I'm sure others can bring something interesting to the table.
>
>
Shared mutable state is one. With an unavoidably-shared resource, like a network connection or a handle to a window on a screen, reference semantics are often what you want.
> On Wed, Nov 23, 2016 at 1:07 PM Dan Appel <dan.appel00 at gmail.com <mailto:dan.appel00 at gmail.com>> wrote:
> Hello everyone!
>
> I was unable to make the kick-off meeting for the HTTP sub-team, but I looked over the meeting notes <https://docs.google.com/document/d/1SWK0qBDi-9DeLJwHlcXcPU7h22JU-DWW8HTVuHbTQiw/edit> and found some topics that I think could use some more on-the-record discussion.
>
> A few questions that I wanted to raise:
>
> 1. Do we want to use concrete types or protocols for Request/Response?
> 2. If we use concrete types, do we want value or reference semantics?
> 3. When is it more convenient to have reference semantics?
> 4. Are there problems that can't be solved with value semantics?
>
> I would like to avoid bike-shedding, and I think this can be done by providing real examples rather than just talking about the pros and cons.
> --
> Dan Appel
> --
> Dan Appel
> _______________________________________________
> swift-server-dev mailing list
> swift-server-dev at swift.org <mailto:swift-server-dev at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-server-dev <https://lists.swift.org/mailman/listinfo/swift-server-dev>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-server-dev/attachments/20161207/35879b7d/attachment.html>
More information about the swift-server-dev
mailing list