Vampire
02/12/2022, 8:09 PMpublic interface Transformer<OUT, IN> {
OUT transform(IN in);
}
And a Java method
filter(Transformer<String, String> var1)
And Kotlin wrongly assumes the second parameter to be non-null
.
How can you call it with a lambda that returns null
?Joffrey
02/12/2022, 8:18 PMVampire
02/12/2022, 8:23 PMplugins {
`java-library`
}
tasks.processResources {
filter { it.ifBlank { null } }
}
Closure
variant, so I can do
tasks.processResources {
filter(object : Closure<String>(null, null) {
fun doCall(line: String) = line.ifBlank { null }
})
}
but the question is more general.
In other situations there might not be an appropriate alternative.ephemient
02/12/2022, 9:49 PMVampire
02/12/2022, 9:54 PMsubprojects/core-api/src/main/java/org/gradle/api/package-info.java
, not subprojects/core-api/src/main/java/org/gradle/api/file/package-info.java
but yeah.
That explains where still wrong non-nullness comes from.
The JavaDoc clearly says that returning null
is a valid option and will delete the transformed line.
So it is a Gradle bug.
But the question remains, can I somehow trick the Kotlin compiler so that null
is returned without writing a Java or Groovy source file that returns null
on behalf?ephemient
02/12/2022, 10:24 PMClosure<*>
API isn't provided:
groovy.util.Eval.xy(this, { line: String -> line.ifBlank { null } }, "x.filter { y.invoke(it) }")
(admittedly 🤢)Vampire
02/13/2022, 12:55 AMtasks.processResources {
filter { it.ifBlank { Supplier { null }.get() } }
}
Jason5lee
02/13/2022, 4:49 AM@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
inline fun <T> `null but only when you know what you're doing`(): T =
// This won't throw because all casting to generics is unchecked!
null as T
Vampire
02/13/2022, 12:12 PMtasks.processResources {
filter { it.ifBlank { null } }
}
it actually works just fine.ephemient
02/14/2022, 7:07 AM@javax.annotation.Nonnull
public @interface NonNullApi
and javax.annotation.Nonnull
is one of the annotations that Kotlin is supposed to recognize, but if can't see that far…