Search code examples
kotlinkotlin-null-safetygeneric-function

How to pass a non-nullable type to a function with a nullable parameter?


A nullable type can be checked and cast to a non-nullable type, but the other way around is quite difficult.

For example:

str1: String? = ""
// if str1 not null
str2: String = str1

str3: String? = str2 // This works!

In my case I'm talking about a function with an Array parameter in which the contents could be null, but the contents of the Array I want to pass to the function is non-nullable. That does not work:

var var1: Array<String?> = arrayOfNulls(10)

var var2: Array<String> = var1.filterNotNull().toTypedArray()

var var3: Array<String?> = var2 // This does not work!

My question is how to make this work?

I am able to cast the array to a nullable array, but I'm not sure if this is the correct way of doing this.


Solution

  • In Kotlin, arrays are invariant, which means that an array of a given type can't be assigned to an array of its parent type. That's why you can't assign Array<String> to Array<String?>, even if String is a subclass of String?. For this to work, you should either

    1. use lists, which are covariant:
    var var1: Array<String?> = arrayOfNulls(10)
    var var2: List<String> = var1.filterNotNull()
    var var3: List<String?> = var2  // Now works
    
    1. cast the array to a nullable array, as you're doing:
    var var1: Array<String?> = arrayOfNulls(10)
    var var2: Array<String> = var1.filterNotNull().toTypedArray()
    var var3: Array<String?> = var2 as Array<String?>  // Now works