3

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.

4
  • 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. Commented Feb 18, 2022 at 11:54
  • For runtime parsing/conversion from String to target type. Commented Feb 18, 2022 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. Commented Feb 18, 2022 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<KClass, (String) -> 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. Commented Feb 18, 2022 at 14:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.