any elegant way to get `z = null` here instead of ...
# getting-started
f
any elegant way to get
z = null
here instead of an exception? (try)
Copy code
fun main() {
    val (x, y, z) = "A/B".split("/")
}
e
Copy code
val (x, y, z) = "A/B".splitToSequence("/")
    .plus(sequence { while (true) yield(null) })
    .take(3)
    .toList()
👍 1
k
Copy code
val (x, y, z) = "a/b".split("/").let { (it + List(3 - it.size) { null }) }
👍 1
f
thanks, both good suggestions
e
Copy code
class ListDestructuredOrNull<out E>(val list: List<E>)
operator fun <E> ListDestructuredOrNull<E>.component1(): E? = list.getOrNull(0)
operator fun <E> ListDestructuredOrNull<E>.component2(): E? = list.getOrNull(1)
operator fun <E> ListDestructuredOrNull<E>.component3(): E? = list.getOrNull(2)
operator fun <E> ListDestructuredOrNull<E>.component4(): E? = list.getOrNull(3)
operator fun <E> ListDestructuredOrNull<E>.component5(): E? = list.getOrNull(4)
operator fun <E> ListDestructuredOrNull<E>.component6(): E? = list.getOrNull(5)
operator fun <E> ListDestructuredOrNull<E>.component7(): E? = list.getOrNull(6)
operator fun <E> ListDestructuredOrNull<E>.component8(): E? = list.getOrNull(7)
operator fun <E> ListDestructuredOrNull<E>.component9(): E? = list.getOrNull(8)
operator fun <E> ListDestructuredOrNull<E>.component10(): E? = list.getOrNull(9)
fun <E> List<E>.destructuredOrNull() = ListDestructuredOrNull(this)

val (x, y, z) = "A/B".split("/").destructuredOrNull()
1