Property access and appending null values to not-null collections


#1

Example:

import kotlin.reflect.KProperty


class ResponsibleDelegate<out T>(initializer: () -> T?) {

    private val value by lazy(initializer)

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T =
            value ?: throw NullPointerException()
}

class CarelessDelegate<out T>(initializer: () -> T?) {

    private val value by lazy(initializer)

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T =
            value as T
}


fun main(args: Array<String>) {

    val definitelyNotNull by ResponsibleDelegate { 10 }
    val definitelyNull by CarelessDelegate<Int> { null }

    val values = mutableListOf<Int>()

    try {
        values += definitelyNotNull
        values += definitelyNull
    } catch (ex: NullPointerException) {
        values += 20
    }

    println("Result: $values")
}

There is already a few posts about null delegates values on non-null types. Is this also impossible to prevent? (I ask just out of curiosity)