How to use Kotlin when to find if a string is numeric?
问题 I'd like to use a when() expression in Kotlin to return different values from a function. The input is a String , but it might be parsable to an Int , so I'd like to return the parsed Int if possible, or a String if it is not. Since the input is a String I can not use the is type check expression. Is there any idiomatic way to achieve that? Edit: My problem is how the when() expression should look like, not about the return type. 回答1: Version 1 (using toIntOrNull and when when as requested)