Ben Woodworth
03/17/2022, 7:43 PMx.is<String>()
x.isNot<String>()
x.as<String>()
x.asOrNull<String>()
For one I think it would make chaining nicer. There are definitely times when I do .let { it as String }
on its own line in the middle of a big chain of calls, and was tempted to wrote my own fun <T> Any?.as(): T
but got dissuaded when I need to write `.`as`<...>()`. Mostly just a curiosity though :)Youssef Shoaib [MOD]
03/17/2022, 7:54 PMaddToStdlib.kt
which includes some extensions that are used in that internal code, amongst them is cast
and safeCast
which function as you'd expect, and so even some JB folks might agree that those functions can be useful. Personally, I don't think is and as should be functions by default, but I'd love if the stdlib had cast
and safeCast
ephemient
03/17/2022, 7:58 PMdynamic_cast<T>()
(well really a collection of functions, because they have many types of casts)ephemient
03/17/2022, 7:59 PMString::class.isInstance(x)
String::class.cast(x)
String::class.safeCast(x)
dmitriy.novozhilov
03/18/2022, 7:20 AMcast<T>()
and safeAs<T>()
for simplifying chain calls, but they require compiler support, because without it compiler won't report warnings about definitely unsafe casts, which may lead to errors in code
"" as Int // CAST_NEVER_SUCCEED warning
"" as? Int // CAST_NEVER_SUCCEED warning
"".cast<Int>() // no warning
"".safeAs<Int>() // no warning
dmitriy.novozhilov
03/18/2022, 7:22 AMis
and notIs
functions, there is no much sense in them, because usually boolean expressions are not used in chain calls, and is/!is
syntax looks much more readable then function callsZach Klippenstein (he/him) [MOD]
03/18/2022, 2:41 PMdmitriy.novozhilov
03/18/2022, 2:50 PMstantronic
03/28/2022, 3:09 PMcastOrNull
be more idiomatic/consistent than safeCast
?dmitriy.novozhilov
03/28/2022, 3:20 PM