title: How to pass a type to generic method in Kotlin?
body: <p>I have a generic method like below</p>
  
  <pre><code>private fun &lt;T&gt; getSomething(): T {
      return "something" as T
  }
  </code></pre>
  
  <p>How can I call this method with a variable <code>T</code> type?</p>
  
  <pre><code>val types = arrayListOf&lt;Type&gt;(String::class.java, Boolean::class.java)
  types.forEach { type -&gt;
      val something = getSomething&lt;type&gt;() // Unresolved reference: type
  }
  </code></pre>
  
  <p>At runtime, I don't know what would be generic type <code>T</code>. I am getting the type from <code>types</code> and should pass it with generic <code>getSomething</code> method. </p>
  
  <h3>Use case</h3>
  
  <p>I want to call database which has several table. Example models are like this</p>
  
  <pre><code>class User{
  
  }
  
  class Student{
  
  }
  </code></pre>
  
  <p>Since all the calling queries are basically same, I want to have generic method for calling database and get data. </p>
  
  <pre><code>private fun &lt;T&gt; getData(model: String): List&lt;T&gt;?{
      return when(model){
          "user" -&gt; getUsers()
          "student" -&gt; getStudents()
          else -&gt; null
      }
  }
  </code></pre>
  
  <p>So when I call above method. Within my loop I want to pass <code>Type</code> as either <code>User</code> or <code>Student</code>.</p>
  
  <pre><code>val types = arrayListOf&lt;Type&gt;(User::class.java, Student::class.java)
  types.forEach { type -&gt;
      val data = getData&lt;type&gt;(type.javaClass.simpleName) // Unresolved reference: type in &lt;type&gt;
  }
  </code></pre>
  
  <p>How can I achieve it.</p>