we already have nice `to<Type>OrNull()` stri...
# stdlib
b
we already have nice
to<Type>OrNull()
string extensions https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/string-to-number.md But don't you think it would a good idea to add generalized version of 'try-or-null' to stdlib? like:
Copy code
val date = tryOrNull { myAwesomeDateTimeParser.parse("2017-13-32") }
which is equal to:
Copy code
val date = try { myAwesomeDateTimeParser.parse("2017-13-32") } catch(e: Exception) { null }
👍 1
v
brk: catching the exception has awful performance and is an anti-pattern
In cases like yours I think it is better to try and find an API which does not throw exceptions
b
yeah, i know, but in some cases it's reasonable. just like `toIntOrNull()`: I bet you don't want to know the exact reason every time you string doesnt fit into an int
v
toIntOrNull()
doesn't catch an exception
we spoiled a lot of blood to get it 😉
The general rule is that you don't put nice easy-to-use methods into stdlib if they have performance problems
🙌 2
b
oh, thanks, got it. just curious: how times does it slower to use
try {x.toInt()} catch { null }
instead of current implementation of
toIntOrNull()
which parses string by its own?
v