1

In C, although we declare a value as const int a = 5;, we can pass &a to a function declared as void someFun(const int *);.

As a rule of thumb, in C, when the original value is need not to be changed, i) if the size of object is less than or equal to size of pointer, we pass it by value, ii) otherwise we pass it by const reference copying the entire value to a function would take more resources.

But in swift, even though an inout parameter is not modified in a function, we can't pass a value declared as let a = 5 to function declared as someFun(_ z: inout Int) -> (). Hence we have to mark z in the function as let. This will copy the entire value to the function. This may cost more if the size of the type of a is big. Is there a workaround to this?

Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
  • 1
    Thats a very keen observation, +1 to question, but if I may put my two cents in when you use inout you are using pass by reference and not by value, hence you have a memory address to the variable/constant named a (which is currently holding the value 5) Now that you have a raw address, there is nothing stopping you from modifying the value it holds. You can simply change it to any value in range Int.min to Int.max, if swift allows passing let in out, someone might be assuming the value of a will never change yet someone might change its value in background using `inout` hence forcing you – Sandeep Bhandari Feb 09 '21 at 08:02
  • to use `var` instead of `let` kind a makes sense and also keeps the intent very clear – Sandeep Bhandari Feb 09 '21 at 08:03
  • In swift, inout capability does not send the reference to the function, its still pass by value, please read >> https://docs.swift.org/swift-book/LanguageGuide/Functions.html – rana5ohaib Feb 09 '21 at 08:10
  • In practice an `inout` value type parameter which is not going to be modified (goes `in` but does not go `out`) makes no sense. – vadian Feb 09 '21 at 08:12
  • @rana5ohaib: As per docs As an optimization, when the argument is a value stored at a physical address in memory, the same memory location is used both inside and outside the function body. The optimized behavior is known as call by reference; link https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID545 – Sandeep Bhandari Feb 09 '21 at 08:18
  • Basically its done by swift itself, as a good practice we should never rely that its always going to be optimized automatically. Also the type of the passed value in the question above is an integer. Also, the passed argument is constant, even at its own scope outside the function, one cannot ever possibly **change** the value of the constant. – rana5ohaib Feb 09 '21 at 08:36

1 Answers1

0

As to my understanding,

The 'inout' in swift does not actually send the reference of the property to the function. When you change the value of the property passed as 'inout', the swift on its own end checks if you have changed the value and then performs that change to the actual property on its own. So the only thing 'inout' does in swift is changing the value of the actual property. So even if it allows you to send a constant as 'inout', you still won't be able to make any use of it, because of the very nature of the 'constant'. You just cant change the value of a 'constant', because its immutable.

Reference Link: https://docs.swift.org/swift-book/LanguageGuide/Functions.html

Edit: As mentioned by @SouravKannanthaB in the comment above, swift can automatically optimize the inout by mimicking the behavior of pass by reference, but i don't believe one can force the optimization to take place.

rana5ohaib
  • 146
  • 4
  • Ohh.... Then every time values are copied and passed to the function. If the value struct is big, then won't it simply become more tedious to copy it? – Sourav Kannantha B Feb 09 '21 at 08:31
  • 1
    As mentioned by @SouravKannanthaB in the comment above, swift can automatically optimize the inout by mimicking the behavior, but i don't believe one can force the optimization to take place. – rana5ohaib Feb 09 '21 at 08:38
  • this is the actual answer for my question.. please edit your original answer to include this point. – Sourav Kannantha B Feb 09 '21 at 09:12