Skip to content

Instantly share code, notes, and snippets.

@philnguyen
Created October 9, 2021 20:57
Show Gist options
  • Save philnguyen/223dc9efc2b2a2d37e377dbaa4ac7fb2 to your computer and use it in GitHub Desktop.
Save philnguyen/223dc9efc2b2a2d37e377dbaa4ac7fb2 to your computer and use it in GitHub Desktop.
Unsound Kotlin
object Test {
@JvmStatic
fun main(args: Array<String>) {
val strs = supplier("foo", "bar")
val ints = supplier(1, 2)
mergeSuppliers(strs, ints)
val s = strs()[2] // boom!
}
private fun mergeSuppliers(vararg suppliers: () -> MutableList<*>) =
mergeLists(suppliers.map { it() }) // shouldn't type-check, but does
private fun<T> mergeLists(lists: List<MutableList<T>>) {
if (lists.size >= 2)
lists[0].addAll(lists[1])
}
private fun<T> supplier(vararg xs : T): () -> MutableList<T> =
xs.toMutableList().let { l -> { l }}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment