[HTTP] Value vs Reference Types


#1

I personally prefer using value type.

First of all only because the request is value type does not mean the underlying members has to be value type. By using getter and setter we can easily provide consistent experience.

Comparing reference type and value type, value types offer the following benefits:

1. More flexible:
When the payload of the request carries is extremely small, there's no reason we should allocate the payload in the heap (and small blocks are also harder to reuse). If we use value type for request, we can make the internal storage abstract by using protocol and only allocate in heap when we need to.

2. More secure:
Value type destroys when it go out of scope, which is not as likely to suffer from memory leak. Therefore more user friendly.

3. Can be use as template:
value type: copy the template struct to the stack, modify a few members, good to go, automatically destroy after.
Reference type: allocate a new class in heap, call constructor, free memory from heap after.

4.Match the Swift standard library.


(John McCall) #3