You are currently viewing Kotlin Generics Help Needed

Kotlin Generics Help Needed

Hi! I’m trying to create a

class

that accepts a

class parameter

and its print function prints the name of

provided class

.


class A<T>(val t: T) { fun print() { println(t!!::class.java.simpleName) } } class B {} fun main() { val b = B() A(b).print() }

The output is

B

But in this scenario, I had to construct

B

first, and then pass it as a parameter.

I don’t want this, I want my class to be modified to accept a

class

parameter instead so that I can run it like.


fun main() { A(B::class).print() }

submitted by /u/JuicyBeauty
[link] [comments]