Skip to content

Solutions should not promote usage of !! #31

@alban-auzeill

Description

@alban-auzeill

For example in Properties/Lazy property/src/Task.kt the solution:

class LazyProperty(val initializer: () -> Int) {
    var value: Int? = null
    val lazy: Int
        get() {
            if (value == null) {
                value = initializer()
            }
            return value!!
        }
}

Would be safer using:

class LazyProperty(val initializer: () -> Int) {
    private var value: Int? = null
    val lazy: Int
        get() = value ?: initializer().also { value = it }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions