<https://twitter.com/kotlin/status/117718354970466...
# announcements
u
https://twitter.com/kotlin/status/1177183549704663041 Consider providing a paired function returning 'null' together with one throwing an exception. Similar to 'toIntOrNull' accompanying 'toInt'. https://t.co/ZkpOljWO2I #KotlinTips https://t.co/SYg5kcr5sN Twitter
🤦🏼‍♂️ 13
👍 2
😖 3
a
Copy code
Welcome to Kotlin version 1.3.50 (JRE 1.8.0_202-b08)
Type :help for help, :quit for quit
>>> "123".toInt()
res0: <http://kotlin.Int|kotlin.Int> = 123
>>> "123".toIntOrNull()
res1: <http://kotlin.Int|kotlin.Int>? = 123
✔️ 2
w
I’m curious, why so many facepalms?
k
"123".toIntOrNull() != null
w
😄 Right, I totally missed that! Thought it’s something about it being disliked convention
m
they deleted this and posted a corrected version 😄
n
I think the face palms come from the fact that null doesn't clearly indicate what error has occurred, and it is bad practise to use
null
when there are much better ways to represent default values. Strongly prefer the
toInt()
option which encourages good practise, and will not cause the program to terminate. In the worst case scenario the program will crash but will continue to run, which is very important for industrial software.
k
No, the facepalms are because the code sample has a mistake like I said.
👍 1
Both options are fine to use, it's clear that
null
means it doesn't parse correctly, it's not like you're throwing away important exception information.