I am trying to find the Kotlin equivalent to this Java return:
return new int[] {1, 2, 3};
I've tried just declaring then returning like this:
val returnArr: IntArray = intArrayOf(1, 2, 3)
return returnArr
But I get a warning that says "Variable used only in following return and can be inlined". What exactly is meant by inline? Is there a way to do this all on one line?
In the context of the warning, "inlining" means removing local variables, parameters, functions, etc. that really don't need to be there, and the code would be simpler without them. For example, in the statement
val returnArr: IntArray = intArrayOf(1, 2, 3)
return returnArr
returnArr
serves no purpose (except perhaps when debugging), and you could replace it with
return intArrayOf(1, 2, 3)
The use of the term "inlining" to refer to such simplifications is a JetBrains convention, I think, due to the existence of the "Inline" refactoring that can automate simplifications like the one we're talking about (right-click on returnArr
and select Refactor/Inline).
The warning is not a compiler warning; it is one of many style inspections that IntelliJ does. If you like your temporary variables, I think you can disable it by going to Preferences/Editor/Inspections/Kotlin/Redundant Constructs and unchecking "Unnecessary local variable".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With