https://kotlinlang.org logo
#konsist
Title
# konsist
p

PoisonedYouth

10/15/2023, 11:29 AM
I have the following requirement that currently is not working as expected. This is a sample file to show the behavior:
Copy code
class Test {

    fun sampleFunctionWithGenericReturnType(): List<Int> {
        return emptyList()
    }

    fun sampleFunctionWithoutGenericReturnType(): Int{
        return 0
    }

}
I want to verify that a specified set of functions have a specified return type. This is working for simple types but as soon as I use generics the check is no longer working.
Copy code
// This works
projectScope
   .classes()
   .functions()
   .assert {
      it.hasReturnTypeOf<Int>()
   }

// This does not work
projectScope
   .classes()
   .functions()
   .assert {
      it.hasReturnTypeOf<List<Int>>()
Is this expected behavior or are generics not yet supported?
i

igor.wojda

10/15/2023, 12:22 PM
Generics are always a pain and they are erashed by the JVM. Maybe
reified
will help. We will investigate. Thx for reporting. https://lemonappdev.atlassian.net/browse/KON-559
p

PoisonedYouth

10/15/2023, 5:15 PM
Thanks for the fast response.
i

igor.wojda

10/16/2023, 9:45 AM
Hey Generic are har to capture. On top of that Kotlin build in type don't have explicit imports, so it is harder to compare types. String comparision will have to do for now:
Copy code
projectScope
   .classes()
   .functions()
   .assert {
      it.hasReturnType { returnType ->
                    returnType.name == "List<Int>"
                }
    }
We will get back to this in the future
p

PoisonedYouth

10/16/2023, 10:27 AM
That's also my temporary solution for now. It just feels a bit strange to compare types by String
i

igor.wojda

10/16/2023, 11:19 AM
I am with you. Under the hood this is a complex problem to solve. You should be able to debug this quite easily by checking out a Konsist project and adding your test. Just look for a test using 'getSnippetFromFile' and add your test (code snippets are in kttxt files and file name is passes to this metgod). Play with this a bit and you will see.
e.g. add test in
KoScopeForKoInterfaceDeclarationTest
2 Views