Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solutions should not promote usage of !! #31

Open
alban-auzeill opened this issue Sep 30, 2021 · 0 comments
Open

Solutions should not promote usage of !! #31

alban-auzeill opened this issue Sep 30, 2021 · 0 comments

Comments

@alban-auzeill
Copy link

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 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant