https://kotlinlang.org logo
#codereview
Title
# codereview
c

christophsturm

10/10/2022, 1:27 PM
Copy code
inline fun <reified C> name() = C::class.simpleName

name<Collection<String>>()
this returns “Collection”. is there a way make it return “Collection<String>“?
j

Joffrey

10/10/2022, 1:29 PM
You lose the element type information as soon as you do
C::class
, because the
KClass
type simply doesn't have that information. What you need is
KType
, which you can get from a reified
C
using
typeOf<C>()
. I don't know off the top of my head how to get a nice string for its name from the
KType
, but you might be able to figure that out
c

christophsturm

10/10/2022, 1:30 PM
ah typeOf was exactly what I need, thanks
e

ephemient

10/10/2022, 1:32 PM
KType.toString()
will get you
kotlin.collections.Collection<kotlin.String>
if you want the simple names you'll have to walk through it yourself, I don't think there's a built-in utility for it
2 Views