In Scala one can extract array elements in a pattern matching statement. For example, I want to take the first two elements of a string input:
private def parseFieldSize(s: String): Option[(Int, Int)] =
try {
s.split(" ").map(_.toInt) match {
case Array(x, y, _*) => Some(x, y)
case _ => None
}
} catch {
case _: NumberFormatException => None
}
How do I do the same in Kotlin? The closest I can get is:
private fun parseFieldSize(s: String): Pair<Int, Int>? =
try {
val inputInts = s.split(" ").map { it.toInt() }
if (2 == inputInts.size)
Pair(inputInts[0], inputInts[1])
else
null
} catch (e: NumberFormatException) {
null
}