Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a value typed variable is copied when it is passed to a function, what hold this copy?

Swift's string type is a value type. If you create a new String value, that String value is copied when it is passed to a function or method, or when it is assigned to a constant or variable.

It is copied when assigned to a constant or variable, this makes sense for me. But when a value typed variable passed to a function it also get copied, this confuses me.

Question

How it gets copied when passing a value typed variable to a function? what kind of "space" holds this copy? Is it some sort of temporary variables invisibly created behind the scene and after the process of the function it gets destroyed?

Thanks

like image 366
SLN Avatar asked Aug 17 '16 14:08

SLN


People also ask

What is copy by value and copy by reference in JavaScript?

When you copy an object b = a both variables will point to the same address. This behavior is called copy by reference value. Strictly speaking in Ruby and JavaScript everything is copied by value. When it comes to objects though, the values happen to be the memory addresses of those objects.

Does object assign pass by reference?

Objects are assigned and copied by reference. In other words, a variable stores not the “object value”, but a “reference” (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object itself.

Is C# copy by value or reference?

By default, C# does not allow you to choose whether to pass each argument by value or by reference. Value types are passed by value. Objects are not passed to methods; rather, references to objects are passed—the references themselves are passed by value.


2 Answers

When passing value type to a function, think of it like assigning it to a local variable whose scope is that function, so the copy behavior is analogous to just assigning a new local variable.

Regarding where it is copied, we should recognize that the copy behavior is actually more complicated than it sounds. As they point out in Building Better Apps with Value Types in Swift (WWDC 2015, Session 414), "Copies are Cheap":

Copying a low-level, fundamental type is constant time

  • Int, Double, etc.

Copying a struct, enum, or tuple of value types is constant time

  • CGPoint, etc.

Extensible data structures use copy-on-write

  • Copying involves a fixed number of reference-counting operations

  • String, Array, Set, Dictionary, etc.

Regarding that last point, behind the scenes Swift does some sleight of hand that avoids copying extensible value types every time they're referenced, but rather just points to the original reference but keeps track of how many references there are and actually only makes copies (a) upon write; where (b) there's more than one reference. This behavior is discussed in more detail in that video.

like image 81
Rob Avatar answered Oct 14 '22 17:10

Rob


Value types in Swift get copied, however in most of the cases only some minor details get duplicated, most of the data remains the same. Take the example of a String value. When passing the value to a function some information get's copied, like the actual pointer to the chars buffer, or its length, but the actual data just gets passed along. This means that passing by value in Swift is a fast operation.

Swift clones the actual data when it detects that it needs to write on it. For example if an array is passed from a let to a var no major copies are made. If however you update the new array by appending a new element, then at that point a clone of the array contents is created, and the new element is added there.

like image 30
Cristik Avatar answered Oct 14 '22 18:10

Cristik