Hi folks. I am having a behavior in Kotlin that do...
# announcements
e
Hi folks. I am having a behavior in Kotlin that does not look right for me. So I have declared the following:
Copy code
val title: (locale: Locale) -> String? = {
    getPrompt(it.language)
}

fun getPrompt(string: String) : String? {
    return if (string == "A") null
    else "A"
}
getPrompt
can return null, hence the
String?
However, the following check returns a warning from the compiler ("the comparation can be simplified since title is never null")
Copy code
if (title == null) {

}
But title can effectively be null. Is this a bug on AS/JetBrains?
s
title is a function
e
Ah, yeah. Thanks, Johannes. Not enough coffee.
s
Maybe you mean
Copy code
if (title() == null){
}
a
what are you setting title to? that is a function so never null, you can try to invoke it with ()
e
You are absolutely right, title(parameter) triggers the right behavior.
Thank you so much.
k
title is really confusing name for a function, no?