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
voddan
07/07/2017, 8:21 AM
brk: catching the exception has awful performance and is an anti-pattern
voddan
07/07/2017, 8:23 AM
In cases like yours I think it is better to try and find an API which does not throw exceptions
b
brk
07/07/2017, 8:23 AM
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
voddan
07/07/2017, 8:23 AM
toIntOrNull()
doesn't catch an exception
voddan
07/07/2017, 8:24 AM
we spoiled a lot of blood to get it 😉
voddan
07/07/2017, 8:25 AM
The general rule is that you don't put nice easy-to-use methods into stdlib if they have performance problems
🙌 2
b
brk
07/07/2017, 8:31 AM
oh, thanks, got it. just curious: how times does it slower to use