liveBook · Manning

isMarkedNullable

As explained in Kotlin in Action, 2E

isMarkedNullable is a Kotlin reflection property that indicates whether a reflected type is declared nullable.

isMarkedNullable is a Kotlin reflection property on a reflected type. It reports whether that type is declared nullable, helping deserialization distinguish an allowed null from a missing required value. section 12.2.5

Checking required constructor parameters

private fun ensureAllParametersPresent(arguments: Map<KParameter, Any?>) { for (param in constructor.parameters) { if (arguments[param] == null && !param.isOptional && !param.type.isMarkedNullable) { throw JKidException("Missing value for parameter ${param.name}") } } } The check allows a missing optional parameter to use its default value, and allows null for a nullable parameter. Only a missing, non-optional, non-nullable parameter causes an exception. section 12.2.5

Listing 12.14 Validating that required parameters are provided
private fun ensureAllParametersPresent(arguments: Map<KParameter, Any?>) {
   for (param in constructor.parameters) {
           if (arguments[param] == null &&
                   !param.isOptional && !param.type.isMarkedNullable) {
           throw JKidException("Missing value for parameter ${param.name}")
       }
   }
}

Inspect the complete validation function, including the relationship between arguments, optional parameters, nullable types, and deserialization errors. section 12.2.5

How Kotlin in Action, 2E covers it

How deep it goes
Explains its role in a validation condition and contrasts nullable, optional, and required parameters.
Examples
Demonstrates ensureAllParametersPresent and its exception for missing non-optional, non-nullable constructor parameters.
What it assumes
Assumes familiarity with Kotlin reflection, constructor parameters, optional parameters, and nullable types.
What you can do after
Apply it via Kotlin reflection to decide whether missing constructor arguments may receive null during deserialization.
How it connects
Connects Kotlin reflection with nullable types, default parameters, constructor parameters, and JSON deserialization.

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.