https://kotlinlang.org logo
#getting-started
Title
# getting-started
r

Rob Elliot

03/01/2023, 5:50 PM
Probably being thick... I need to construct a boxed inline class from Java (well, Groovy, but kind of the same thing...) in order to put it in a list. I can't currently work out how?
e

ephemient

03/01/2023, 6:19 PM
it's not quite the same thing, since Groovy can more easily access things that Java can't
Copy code
$ groovysh -cp kotlin-stdlib-1.8.10.jar
groovy:000> kotlin.UInt."box-impl"(-1)
===> 4294967295
but perhaps reconsider if you have to do this. why do you have a list of boxed inline classes and why does it need to be used outside of Kotlin?
r

Rob Elliot

03/01/2023, 7:07 PM
I've got a Kotlin function that returns
List<ValueType>
and I need to test it in Spock, hence Groovy calling Kotlin. I'm not sure if I can avoid them being boxed while keeping the Kotlin type... all I'm doing is
listOf("1", "2").map { ValueType(it) }.toSet()
and that seems to box them.
But thanks, that's a good point on groovy being able to call members with weird names, that works!
e

ephemient

03/01/2023, 7:19 PM
you could build your own specialized
ValueTypeSet
(as
UIntArray
does for
UInt
) that is represented by a
Set<String>
or whatever the
val
you're wrapping is
but that'll be pretty awkward, at least until https://youtrack.jetbrains.com/issue/KT-44654
r

Rob Elliot

03/01/2023, 7:22 PM
Honestly I could probably just use a data class - it's not like the memory is actually an issue, I just quite like the idea of compiler enforced type aliases!
3 Views