Since value classes (aka inline classes) are not classes at runtime but the value class, is it possible to make a list of them, and the list is bound to the list of values?
If i couldn't explain that very clearly, here is a code example:
class Foo(var i)
@JvmInline
value class FooWrapper(val foo: Foo)
fun main() {
val fooList = mutableListOf(Foo(1), Foo(2))
val wrappedFooList = fooList.???<FooWrapper>()
// fooList and wrappedFooList are the same list at runtime, so when you insert a value to fooList, it gets "added" to wrappedFooList as `FooWrapper(added)`
// This is what im currently using, but this is a seperate list, so when a value gets inserted into fooList, it doesn't get inserted here.
val wrappedFooListButNotTheSameList = fooList.map { FooWrapper(it) }
fooList.add(Foo(3)) // FooWrapper(Foo(3)) now exists in wrappedFooList
}
This is only true sometimes. Inlining does not always happen, and the class file for the inline class does exist.
As soon as you start using generics, inlining goes out of the window. That is, your list of
FooWrapperwould not be inlined at all.Documentation:
See also the code sample that follows that. This is likely because when they are used as another type, code that doesn't know about the inline class is likely going to be interacting with the wrapper, and unexpected behaviours would occur if they are not boxed.
With all that in mind, if you still want two lists of unrelated types, that are "linked" together, you can first encode the conversion between the types with an interface:
Then make a
ConvertList<T, U>and aConvertListIterator<T, U>by delegating everything (yes this is a lot of boilerplate). The built-inbycan't help here because you are also adding an extra.convertedon everyUvalue. Instead of the interfaces, you can also addT.() -> UandU.() -> Tin the constructor parameters.Usage: