1

Kotlin specification states basic types as "Some types can have a special internal representation - for example, numbers, characters and booleans can be represented as primitive values at runtime". https://kotlinlang.org/docs/basic-types.html

Yet, I am not figuring out any way to know if the Type representation in Kotlin, i.e. instance of KClass is a basic type, or not?


Scope

We need to parse and convert String values to strongly typed instances and for "basic types" we want to use the corresponding to<basic type> auxiliary method, like Java valueOf or boxing.

Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94
  • 1
    Can I ask what do you need this for? One thing to understand is that these basic types are not guaranteed to have a primitive representation. They only could be represented in such a way, but it depends on the target and other circumstances. – broot Feb 18 '22 at 11:54
  • For runtime parsing/conversion from String to target type. – Miguel Gamboa Feb 18 '22 at 12:30
  • Hmm, but if you want to use a different `to*` function per each type, then I guess you need to explicitly reference each such type anyway - to choose a specific function. Of course you can use reflection to find the function, but it sounds like an overkill. There are just few such types. – broot Feb 18 '22 at 12:40
  • I agree with you. Even so we have to map those types to corresponding `to*` functions and store them somewhere, e.g. in a `basicParsers` of `Map Any>`. Then, we need to decide whether to go, or not, to that `basicParsers` depending on the target's type. Off course, we may delegate on the result of finding or not a parser in `basicParsers`. But I would prefer to only check that map, if I was really in the presence of a basic type. – Miguel Gamboa Feb 18 '22 at 14:53

0 Answers0