Convert a nullable type to its non-nullable type?
You need double exclamation mark like so:
val bar = makeNewBar(foo.name!!)
As documented in Null Safety section:
The third option is for NPE-lovers. We can write b!!, and this will return a non-null value of b (e.g., a String in our example) or throw an NPE if b is null:
val l = b!!.length
Thus, if you want an NPE, you can have it, but you have to ask for it explicitly, and it does not appear out of the blue.
You could use an extension:
fun <T> T?.default(default: T): T {
return this ?: default
}
Then use it like this:
fun getNonNullString(): String {
return getNullableString().default("null")
}