0

Given the discussion that took place : Click

Finally , This method received the most scores :

var x = 10 var y = 20 fun main() { println("x=$x, y=$y") // x=10, y=20 swap(::x, ::y) println("x=$x, y=$y") // x=20, y=10 } fun <T> swap(firstRef: KMutableProperty0<T>, secRef: KMutableProperty0<T>) { val temp = firstRef.get() firstRef.set(secRef.get()) secRef.set(temp) } 

But as one user commented at that time, this method gives me the following error: "References to variables and parameters are unsupported."

Do we still have this problem in Kotlin and there is no solution??[I'm new to Kotlin]

1
  • 1
    Is there a real-world problem you're trying to solve by using such references?  (The classic swap-two-variables problem is easy to follow, but of little practical benefit.  And as Tenfour04 says, reflection is a very strong code smell unless you're writing frameworks, libraries, compiler tools, or similar.) Commented Jul 15, 2024 at 22:38

1 Answer 1

3

That works with properties, not local variables. And you need to have imported the reflection library. And furthermore, this strictly answered the question the OP posed without mentioning that it is not something you would typically do in general application code. If you’re using reflection as someone new to OOP or strongly typed languages, you probably have a fundamental misunderstanding of how to use the language. Reflection is rarely used outside of building specific code libraries that do powerful code modification and generation.

The correct way to swap variables in Kotlin is in one of the other answers on that question, the one that uses the also scope function.

The OP of that question wanted to write a function that modifies what references some properties/variables are using. This is basically like pointer manipulation in lower level languages. It’s typically not recommended in a higher level, strongly typed objected oriented language like this. Poor encapsulation and separation of concerns, among other issues. But there are cases where you might write functions that take mutable classes as parameters and do something to them. It just wouldn’t normally happen in general-purpose sorts of functions.

You asked if Kotlin "still has this problem", but it is not considered a problem, since it's something you typically shouldn't do. So, it's not something that will ever be "solved".

Sign up to request clarification or add additional context in comments.

3 Comments

I am currently switching from C# to Kotlin, and my general approach to learning Kotlin is comparing these two languages. In C#, which is a very object-oriented language, this issue can easily be resolved with ref. Now I wanted to see if Kotlin has a similar feature or not.
When I use C#, I avoid ref for the reasons mentioned above. IMO, it’s a somewhat anti-OOP feature that the designers of C# included because it needed to be interoperable with older languages like C++ for Windows/.net standard library purposes. If you read about the design philosophies of Java and Kotlin, you’ll see that they will never get a similar feature, ever, unless they fundamentally change the design goals. Java and Kotlin are much more opinionated about how you should write code, making it impossible to do things that research has shown to make code less robust.
If you compare C# to C++, you can see they made it far more restrictive, but they were not willing to give up an analog to pointers. In the same way, Kotlin is even more restrictive than Java. For instance, all function parameters are enforced to be final within the function. But interoperability requirements prevented them from “fixing” everything known to cause code fragility, such as constructors calling open functions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.