Any way to tell Kotlin compiler that prior callable function already did a check for null -
if have code fragment like
val mynullablestring : string? val resultingstring = if (mynullablestring.isnullorblank()) mynulldefaultstring else string.format(mynullablestring!!, someotherstring)
i have use mynullablestring!!
in string.format()
since compiler not able figure out isnullorblank()
includes null-check. correct or there way tell compiler function infer instance not null?
didn’t see mentions there way tell such info compiler, here workaround: having function returning null if string empty , use let?.{} after that
inline fun string?.nullifblank(): string? = if (this?.isblank() ?: true) null else val resultingstring: string = mynullablestring.nullifblank()?.let { string.format(it) } ?: mynulldefaultstring
Comments
Post a Comment