Returning null from the Copy filter closure in Kotlin

This is presumably a simple question, but all of my attempts to search for the answer are stymied by the commonality of the search terms.

The filter closure on the Gradle Copy task is defined, in part, like this:

The Closure will be called with each line (stripped of line endings) and should return a String to replace the line or null to remove the line.

But if you actually do that in Kotlin:

      filter { line ->
        if (line.contains("thing i want to suppress")) {
          null 
        } else {
          line
        }

Kotlin complains:

w: .../build.gradle.kts: Type mismatch: inferred type is Nothing? but String was expected

I can see why, but can I reformulate the filter closure so that Kotlin doesn’t make me feel bad? :slight_smile:

Yeah, this is a known problem.

Some related reports that also have some work-arounds for similar cases you could maybe adapt:

This should hopefully fix it properly “some time”:

Ah. Thank you. I’ll just ignore the warning and hope it goes away :slight_smile:

1 Like