roamingthings
07/26/2017, 6:59 AMval myNullableString : String?
val resultingString = if (myNullableString.isNullOrBlank()) myNullDefaultString else String.format(myNullableString!!, someOtherString)
I have to use myNullableString!!
in String.format()
since the compiler would not be able to figure out that isNullOrBlank()
includes a null-check. Is this correct or is there any way to tell the compiler that a function will infer that the instance is not null?pilgr
07/26/2017, 8:15 AMinline fun String?.nullIfBlank(): String? = if (this?.isBlank() ?: true) null else this
val resultingString: String = myNullableString.nullIfBlank()?.let { String.format(it) } ?: myNullDefaultString
roamingthings
07/26/2017, 8:24 AM