isElementOptional

abstract fun isElementOptional(index: Int): Boolean

Whether the element at the given index is optional (can be absent is serialized form). For generated descriptors, all elements that have a corresponding default parameter value are marked as optional. Custom serializers can treat optional values in a serialization-specific manner without default parameters constraint.

Example of optionality:

@Serializable
class Holder(
val a: Int, // Optional == false
val b: Int?, // Optional == false
val c: Int? = null, // Optional == true
val d: List<Int>, // Optional == false
val e: List<Int> = listOf(1), // Optional == true
)

Returns false for valid indices of collections, maps and enums.

Throws

if the current descriptor does not support children elements (e.g. is a primitive).