How to choose method signature?


#1

I bold places where compiler show errors.

class TestName {   Test fun test() {   val all = listOf(javaClass<AnimalFox>(),javaClass<AnimalDog>())   for(tp in all) {            println(Name.name(tp)) // cannot infer type   }   for(tp in all) {            println(Name.name2(tp))   }   } }

abstract class AnimalAbs : Name

class AnimalFox:AnimalAbs()

class AnimalDog:AnimalAbs()

trait Name {
  fun name(): String = Name.name(javaClass)

  class object {
  fun name<T : Name>(tp: Class<T>) =
           if (!Modifier.isAbstract(tp.getModifiers())) {
                   tp.getSimpleName().substringAfter(tp.getSuperclass()!!.getSimpleName().substringBefore(“Abs”)).toLowerCase()
           }else throw Exception("$tp is abstract")

  &nbsp;&nbsp;fun name2&lt;T : Name&gt;(tp: Class&lt;out T&gt;) = // getSuperclass not resolved

           if (!Modifier.isAbstract(tp.getModifiers())) {
                   tp.getSimpleName().substringAfter(tp.getSuperclass()!!.getSimpleName().substringBefore(“Abs”)).toLowerCase()
           }else throw Exception("$tp is abstract")
  }
}


I try to write static method name in trait Name. I have two variatns but both dont compiles.
1)

Error:(14, 26) Kotlin: Type inference failed: Cannot infer type parameter T in fun <T : net.unitcraft.game.Name> name(tp: java.lang.Class<T>): kotlin.String
None of the following substitutions
(java.lang.Class<net.unitcraft.game.AnimalAbs>)
(java.lang.Class<net.unitcraft.game.Name>)
(java.lang.Class<kotlin.Nothing>)
can be applied to
(java.lang.Class<out net.unitcraft.game.AnimalAbs>)


2)

Error:(39, 58) Kotlin: Unresolved reference: getSuperclass


How to fix method Name.name?


#2

This is a problem in Kotlin's type inference that we are fixing at the moment. A workaround could be to provide type arguments for name() expilcitly