Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KClass reference for nullable values

In Kotlin, when declaring getting a KClass for a type, such as String::class (which represents values whose type will be String), is there a syntax to indicate that the value is nullable (ie represente String? values instead of String).

The context is that I'm trying to generate Kotlin classes using KotlinPoet but all properties I create (with PropertySpec.builder) are not nullable (String for instance, when what I actually want is String?).

Thanks for your help.

like image 917
Antoine Avatar asked May 18 '17 16:05

Antoine


1 Answers

No. Kotlin has Nullable types and Non-Null Types but it does not have a concept of nullable classes and non-null classes. A KType is a classifier plus nullability where a classifier can be a KClass.

KotlinPoet 0.1.0 did not have a way to represent nullable types but support for such was added in 0.2.0 via TypeName.asNullable. e.g.:

val type = TypeName.get(String::class).asNullable()
like image 77
mfulton26 Avatar answered Oct 03 '22 07:10

mfulton26