[swift-users] Passing value types or members of value types?

Kelvin Ma kelvin13ma at gmail.com
Sun May 7 00:24:32 CDT 2017


I hear that a lot but why do all the official sources sound like copy-on-write is something you have to manually implement with a class and isUniquelyReferenced if it’s not an array or a string?

> On May 7, 2017, at 12:02 AM, Nate Birkholz <nbirkholz at gmail.com> wrote:
> 
> Let me try one more time, my stupid phone keeps changing words. 
> 
> ACTUALLY, the struct is a pointer until it's written to, and is only copied on write.
> 
> Sent from my iPhone, please excuse brevity and errors
> 
>> On May 6, 2017, at 9:59 PM, Nate Birkholz <nbirkholz at gmail.com> wrote:
>> 
>> Oddly, until it's *written* you will be working with a pointer.
>> 
>> Sent from my iPhone, please excuse brevity and errors
>> 
>>> On May 6, 2017, at 9:33 PM, Kelvin Ma via swift-users <swift-users at swift.org> wrote:
>>> 
>>> If I have a “large” struct like
>>> 
>>> struct Vertex
>>> {
>>>     let x:Double, y:Double
>>>     var r:Int, g:Int, b:Int
>>>     let s:Double, t:Double
>>>     var selected:Bool
>>> }
>>> 
>>> and I want to pass it to a function without modifying it like
>>> 
>>> func taxicab_distance(_ v1:Vertex, _ v2:Vertex) -> Double
>>> {
>>>     return v2.x - v1.x + v2.y - v1.y
>>> }
>>> 
>>> Will the entire struct Vertex get copied and pushed onto the stack, or will only the relevant members be passed to the function?
>>> 
>>> In other words, does the compiler know to optimize this call down to
>>> 
>>> func taxicab_distance(v2x:Double, v1x:Double, v2y:Double, v1y:Double) -> Double
>>> {
>>>     return v2x - v1x + v2y - v1y
>>> }
>>> 
>>> ?
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170507/d163abe7/attachment.html>


More information about the swift-users mailing list