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

Kelvin Ma kelvin13ma at gmail.com
Sat May 6 23:33:02 CDT 2017


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
}

?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170506/9dc915fb/attachment.html>


More information about the swift-users mailing list