Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - Get property type of KProperty1<T, out R>

Given the following code

class Foo(val bar: String)
val p = Foo::bar

How do I obtain the property type String from p?

like image 419
Matthew Layton Avatar asked Oct 12 '25 05:10

Matthew Layton


1 Answers

If by obtaining you mean to check if the property is of type String, you can compare the returnType of the property with any other KType

Example

check(p.returnType == String::class.createType())
like image 162
Omar Mainegra Avatar answered Oct 15 '25 18:10

Omar Mainegra