https://kotlinlang.org logo
#detekt
Title
# detekt
b

Brais Gabin

08/30/2021, 3:15 PM
I have a function (
KtNamedFunction
) and I want to know if the returning type is Unit or not with type solving. I'm using this:
function.bodyExpression.getResolvedCall(bindingContext)?.resultingDescriptor?.returnType?.isUnit()
but I get null for functions like this:
fun asdf() = 5
. And if I use this:
function.getType(bindingContext)?.fqNameOrNull()?.shortName()?.let { it.toString() == "Unit" }
get
null
for
fun asdf() {}
.
g

gammax

08/30/2021, 5:08 PM
My gut feeling tells me the first one is the probably correct one 🤔
t

t-kameyama

08/31/2021, 12:23 AM
Copy code
override fun visitNamedFunction(function: KtNamedFunction) {
    val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, function] as? FunctionDescriptor
    val isUnit = descriptor?.returnType?.isUnit()
}
❤️ 1
b

Brais Gabin

08/31/2021, 2:42 PM
Thanks @t-kameyama! It works as a charm!
2 Views