https://kotlinlang.org logo
#feed
Title
# feed
s

Sam

03/06/2023, 10:27 PM
A few interesting null-safety bugs which you might not know you can still run across in Kotlin: https://link.medium.com/crB736lsXxb
👍 7
c

CLOVIS

03/06/2023, 10:33 PM
That last one has bit me multiple times before in this shape:
Copy code
abstract class Foo {
    abstract val name: String

    init {
        require(name.length < 20)
    }
}

class FooImpl(override val name: String) : Foo()
I haven't had issues with the others, but this one I frequently have
s

Stephan Schröder

03/06/2023, 11:09 PM
interesting stuff, I'm not happy that those null-unsafe niches exist 😑 but I haven't run into any of them yet. I can see case 3 happening, but I'm more concerned about case 2. It's equally likely to cause a NullPointer as a ClassCastException:
Copy code
val listOfLists = listOf(listOf(1))
    val values = listOfLists.filterIsInstance<List<String>>()
    println("Values: $values")
    println(values[0][0].uppercase())
leading to
Copy code
Values: [[1]]
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String
type erasure at its finest.
6 Views