Hello! Is there any planning to include possibilit...
# language-evolution
p
Hello! Is there any planning to include possibility to pass function argument by ref?
v
All function arguments except primitive types are passed by reference, i.e. object reference. If an object is mutable, you can modify its state inside a function.
j
Not really. In fact, like in Java, everything is passed by value. For reference variables (variables of non-primitive types) the value of the variable is a reference to an object, but it is still passed by value. You cannot change the value of a local variable by making a call to function, which passing by reference would allow
v
So, that’s why I need to understand what he means by passing by reference. Actually everything is passed by certain kind of “value”. It could be the actual value (primitive), the value of a an object reference, the value of a reference to a primitive, or the value of a reference to an object reference. It depends on how many indirections between something passed to a function and the corresponding actual object/primitive. Java/Kotlin does not support pass by reference as in C++ using the & declaration. So, you cannot pass a reference to a function and allow the function to modify the value. Passing by reference is general considered a bad design, so Java did not provide the feature right at the beginning and neither does Kotlin. I don’t think it is a good idea to
So, that’s why I need to understand what he means by passing by reference. Actually everything is passed by certain kind of “value”. It could be the actual value (primitive), the value of a an object reference, the value of a reference to a primitive, or the value of a reference to an object reference. It depends on how many indirections between something passed to a function and the corresponding actual object/primitive. Java/Kotlin always passes non-primitive objects by reference (the value of the reference) to a function. You cannot pass a copy of an object like what you can do in C++. For primitive types, Java/Kotlin always passes them as values (copy) to a function. I don’t support the idea of adding pass by reference as provided by C++ to Kotlin. In the new functional programming paradigm, we even discourage the use of mutable objects and avoid side-effect in functions. Why would we want to introduce pass by reference feature to Kotlin?
g
There are no plans about which I’m aware off, I would create a feature request on Kotlin issue tracker (or maybe found existing one), specify your use cases and collect community feedback
k
Suitable use cases can be found in C#. For example, the tryParse function returns a boolean if the given string can be parsed as an integer, and also sets a passed-by-reference int variable to the value. The way Kotlin and Java do it is by using exceptions as flow control, which is a well-known antipattern.
g
Kotlin way is to use toIntOrNull, no exceptions, and I believe it's actually better than have only 1 variable instead of 2 as in case of boolean Also mutability is discouraged to Kotlin as well, so I don't think that it's good argument to encourage it with a new feature This is why I suggest creating a feature request with use cases so everyone could give arguments