// completion order here: X, Y, WHEN_VARIABLE fun List.optimizeReadOnlyList() = when (size) { 0 -> emptyList() // here type variable Y will be fixed to Nothing 1 -> listOf(this[0]) else -> this } fun listOf(element: X): List = TODO() fun emptyList(): List = TODO() fun test(l: List): List { val foo = l.optimizeReadOnlyList() return foo }