andylamax
08/27/2020, 7:27 PM//case A
inline reified fun <T:Any?> T.doThing(name: String = T::class.simpleName ?: "") {}
or
//case B
inline reified fun <T:Any> T?.doThing(name: String = T::class.simpleName ?: "") {}
streetsofboston
08/27/2020, 7:32 PMthis
extension receiver to be nullable or not (and <T : Any?>
can be abbreviated to just <T>
).Zach Klippenstein (he/him) [MOD]
08/27/2020, 7:36 PMreified
to the correct place, I don’t think 1 would compile - KClass
can only refer to non-nullable class typesmuliyul
08/27/2020, 7:36 PMCharSequence?.isNullOrEmpty()
val str: String? = null
if(str.isNullOrEmpty()) { ... }
// vs isEmpty
if(str?.isEmpty() == true) { ... }
// equals required for nullable booleans.
Nir
08/27/2020, 7:45 PMinline fun <reified T:Any> T.doThing(name: String = T::class.simpleName ?: "") {}
Nir
08/27/2020, 7:49 PMNir
08/27/2020, 7:50 PM//case D
inline fun <reified T:Any> doThing(name: String = T::class.simpleName ?: "") {
println(name)
}
Nir
08/27/2020, 7:50 PM