Seems like there's no lowering / optimization for ...
# announcements
a
Seems like there's no lowering / optimization for destructing array declaration of same data type.
Copy code
var (e, f, c, d) = arrayOf(1, 10, 12, 55)
Seems to decompile to:
Copy code
Integer[] var6 = new Integer[]{5, 44, 34};
boolean var8 = false;
int var2 = ((Number)var6[0]).intValue();
var8 = false;
int var3 = ((Number)var6[1]).intValue();
var8 = false;
int var4 = ((Number)var6[2]).intValue();
var8 = false;
int d = ((Number)var6[3]).intValue();
Is there a workaround to declare multiple variables concisely but also with an efficient handling performance wise?
b
I don’t think there is. I also don’t think there needs to be because frankly, code like
var (a, b, c, d, e, f, g) = arrayOf(612, 7652, 721, 67552, 6756, 1235, 75465)
would fail a code review so fast you’d get whiplash. 🙂
💯 1
n
Is the code generated for destructuring a dataclass efficient? I'd assume it's better
In that case you could write tuples of various sizes and a function that creates then
a
@Nir Yea, its better, but I think creating thousands of dataclasses is overkill here, as the bytecode increases by a huge amount for every data class (all methods like component, toString, copy, etc are generated)
n
Thousands?
You mean like ten?
👌 1
a
Still overkill, as an example for 3 ints, its different for 2 int we need different, for double its like....
n
You only need one tuple class for each ariness
Tuple2, Tuple2, Tuple3, etc
(generic)
And then overloaded tuple functions
Annoying but its a one time cost of maybe 50 lines of code
IMHO Kotlin should really have kept built in tuples.. necessary for things like zip
a
If its generic, there is no difference between this array method, since the types are boxed 😛
n
I don't know exactly what goes on in the JVM but there's still a huge difference
In one case you know at compile time how many values there are
As opposed to a dynamically sized array