:question:I have the following requirement that cu...
# konsist
p
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
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
Thanks for the fast response.
i
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
That's also my temporary solution for now. It just feels a bit strange to compare types by String
i
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