Created
October 9, 2021 20:57
-
-
Save philnguyen/223dc9efc2b2a2d37e377dbaa4ac7fb2 to your computer and use it in GitHub Desktop.
Unsound Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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