Generic confusion


#1

 

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


What is means? How to write properly?


#2

I make errs in previous post.

class TestHasName {   Test fun testName() {   val all = listOf(javaClass<EntityTest>(),javaClass<EntityTest2>())   for(tp in all) {            println(Name.name(tp)) // this dont compiles   }   } }

abstract class EntityAbs : Name

class EntityTest:EntityAbs()

class EntityTest2:EntityAbs()

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

  class object {
  fun <T : Name> name(tp: Class<T>) = tp.getSimpleName()
  }
}


Compiler message:

Error:(24, 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.EntityAbs>)
(java.lang.Class<net.unitcraft.game.Name>)
(java.lang.Class<kotlin.Nothing>)
can be applied to
(java.lang.Class<out net.unitcraft.game.EntityAbs>)


How to write this without compile err?


#3

To fix Name.name read http://kotlinlang.org/docs/reference/generics.html#variance


#4

I reasked my question with more details: http://devnet.jetbrains.com/thread/457013