does anybody know how to get the type (KType / KCl...
# announcements
p
does anybody know how to get the type (KType / KClass ?) of the embedded value in a value class?
Copy code
if (klass.isValueClass) {
  // TODO get type of embedded value for further processing
}
update - I think I found it:
Copy code
if (klass.isValue) {
  val ctor = klass.primaryConstructor ?: error("value class has no primary constructor")
  val valueType = ctor.parameters.firstOrNull()?.type ?: error("value class has no single parameter in constructor")
   // use it
}
If somebody knows a shorter way I would appreciate
e
kotlin value classes are planned to allow any number of fields, going along with project valhalla in jvm
https://youtrack.jetbrains.com/issue/KT-1179 actually it seems there's a plan to enable that even without valhalla
p
ah ok - good to know. Is @JvmInline an indication that only a single parameter is allowed?
since I am writing on an typescript generator to map inline classes correctly
e
https://github.com/Kotlin/KEEP/blob/master/notes/value-classes.md#multifield-value-classes-before-valhalla
In fact, there are legitimate use-cases for “inlined” multi-field value classes support on pre-Valhalla JVM. Consider the following example:
Copy code
@JvmInline
value class Complex(val re: Double, val im: Double)
so right now, all value classes are single-parameter, but with or without
@JvmInline
that restriction will probably disappear
p
thanks a lot @ephemient
i really appreciate
🙂
so if I get it right value classes vs. regular classes are all about controlling whether to use heap or stack allocated memory?
e
not how I would put it... it's about object identity.
"is there any distinction between
InlineClass(1)
constructed here versus
InlineClass(1)
constructed there"
if the answer is no, then it enables a number of further optimizations... but it's not really about stack vs heap, JVM will box value classes onto the heap in plenty of situations, K/N can place regular classes on the stack as long as escape analysis works...
p
wow, that makes it a lot clearer on what all it is about … thanks for sharing your knowledge 👍😎