https://kotlinlang.org logo
Title
a

atsushi-koshikizawa

09/26/2019, 11:37 AM
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

wasyl

09/26/2019, 12:12 PM
I’m curious, why so many facepalms?
k

karelpeeters

09/26/2019, 12:16 PM
"123".toIntOrNull() != null
w

wasyl

09/26/2019, 12:18 PM
😄 Right, I totally missed that! Thought it’s something about it being disliked convention
m

Matteo Mirk

09/26/2019, 3:06 PM
they deleted this and posted a corrected version 😄
n

napperley

09/27/2019, 12:48 AM
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

karelpeeters

09/27/2019, 5:39 AM
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.