https://kotlinlang.org logo
Title
d

Dias

12/19/2018, 5:00 PM
Why is
String.toInt()
just an alias for
parseInt()
, but
String.toIntOrNull()
is custom implementation instead of trycatch implementation of parseInt()? Is it because throwing exceptions in java is heavy or something like that?
g

gildor

12/19/2018, 5:23 PM
Yes, this is because of a Throwable that will be created but ignored
👌 1
r

robin

12/20/2018, 4:10 PM
To extend a bit on that, every time* a Throwable is created, it captures the current stack trace, which is pretty costly, and throwing and catching across function boundaries also has some overhead. *actually not every time, see this stackoverflow answer: https://stackoverflow.com/a/36344265/3887813