katz
08/26/2019, 11:18 AMtryOrElses
-> val t = try{...}
<- null on fail val t = try{...} else X
val t = try{} ?: X
raulraja
08/26/2019, 11:30 AMTimmy
08/26/2019, 11:30 AMdmitriy.novozhilov
08/26/2019, 12:28 PMtry
function with such functionalty:
inline fun <R> myTry(block: () -> R): R? {
return try {
block()
} catch (e: Exception) {
null
}
}
fun test() {
val x = myTry { ... } ?: 1
}
katz
08/26/2019, 1:26 PMtryOrElse
- it is suggestion to add such into kotlin coreLeon K
08/26/2019, 2:06 PMtryOrNull
function would help a lot ;Dkatz
08/26/2019, 2:17 PMtry? {}
blockdmitriy.novozhilov
08/26/2019, 2:31 PMrunCatching
in stdlib
usage:
val x = runCatching { ... }.getOrNull() ?: ...
Leon K
08/26/2019, 5:11 PMtry?
as that would add New Syntax without ANY Benefit over an existing function.
If were talking about Syntax changes, id rather have something like this:
fun makeString(): String {
throw Exception()
}
val foo: String? = makeString??()
// foo = null
So a Syntax that can turn any function that could throw into a function that can return null on exceptions. That would be great for chsining tookatz
08/26/2019, 8:19 PMval foo:String? = try?{ makeString() }
katz
08/26/2019, 8:22 PMval x = runCatching {...}
without calling getorNull
... or i am just too lazyjosephivie
08/26/2019, 10:52 PMtry
is already an expression in the language.
val x: Int? = try { someIntExpression() } catch(e:Exception) { null }
Leon K
08/27/2019, 10:20 AMraulraja
08/27/2019, 4:53 PM