title: Kotlin Generics and nullable Class type
body: <p>How do I handle a nullable generics Class type in Kotlin?</p>
  
  <p>Example function with generics:</p>
  
  <pre><code>fun &lt;I&gt; calculateStuff(valueType: Class&lt;I&gt;, defaultValue: I): I {
      // do some work
  
      return defaultValue;
  }
  </code></pre>
  
  <p>Here is a calling function (note the 2nd param for calculateStuff(...))</p>
  
  <pre><code>fun doStuff() {
      // works fine!
      val myVar1 = calculateStuff(String::class.java, "")
  
      // FAIL (null is not accepted... Error: "Cannot infer type parameter I in....")
      val myVar2 = calculateStuff(String::class.java, null)
  }
  </code></pre>
  
  <p>Work-around (change return type to I? AND defaultValue to I?):</p>
  
  <pre><code>fun &lt;I&gt; calculateStuff(valueType: Class&lt;I&gt;, defaultValue: I?): I? {
      return defaultValue;
  }
  </code></pre>
  
  <p>Preferred method, but does not seemed supported by Kotlin (note "String?::class.java"):</p>
  
  <pre><code>val myVar2 = calculateStuff(String?::class.java, null)
  </code></pre>
  
  <p>I really want to be able to send to the method (calculateStuff(...)) the return type, and if it can be null, as the first parameter... that way I ONLY have to null-check the return value if I pass a nullable Class in the first param.</p>
  
  <p>Is this possible to do in Kotlin?</p>