dave08
02/01/2024, 12:48 PMval foo = List<String>
that I want to turn into a list of value class instances, is the compiler smart enough to optimise out the conversion foo.map { FooValueClass(it) }
and skip the map
step? Or will it just box it...Klitos Kyriacou
02/01/2024, 12:55 PMfoo
, just as it would if you did foo.map { it }
. phldavies
02/01/2024, 12:55 PMphldavies
02/01/2024, 12:56 PMList<FooValueClass>
will always contain boxed instancesdave08
02/01/2024, 12:56 PMphldavies
02/01/2024, 12:58 PMList<String>
contains the unboxed values - List<FooValue>
would contain the boxed values. The compiler won't know that the first list would be unused afterwards so wouldn't be able to swap the elements.dave08
02/01/2024, 1:01 PMKlitos Kyriacou
02/01/2024, 1:02 PMString
and FooValue
are both reference types (not primitive), why can't the compiler be clever enough to store the FooValue
items unboxed in the (type-erased) List?dave08
02/01/2024, 1:03 PMdave08
02/01/2024, 1:05 PMfoo
and then made the transformation... who says I won't use foo as a string list...dave08
02/01/2024, 1:06 PMval foo = prefs.getStringPreferences("key", emptySet()).map { FooValue(it) }
would maybe have had more chance to be detected by the compiler...Wout Werkman
02/01/2024, 1:12 PMdave08
02/01/2024, 1:15 PMdave08
02/01/2024, 1:16 PMdave08
02/01/2024, 1:19 PM