title: How to convert Array<T?>? into Array<T> in Kotlin
body: <p>I'm taking my first steps in Kotlin, and trying to write a simple string split function. I started with this:</p>
  
  <pre><code>fun splitCSV(s : String) : Array&lt;String&gt; {
      return s.split(",");
  }
  </code></pre>
  
  <p>Which I guess can be also written like this:</p>
  
  <pre><code>fun splitCSV(s : String) : Array&lt;String&gt; = s.split(",");
  </code></pre>
  
  <p>But I'm getting a type error, since s.split returns an <code>Array&lt;String?&gt;?</code> and not <code>Array&lt;String&gt;</code>. I couldn't find a simple way to do a cast, so I wrote this function to do the conversion:</p>
  
  <pre><code>fun forceNotNull&lt;T&gt;(a : Array&lt;T?&gt;?) : Array&lt;T&gt; {
      return Array&lt;T&gt;(a!!.size, { i -&gt; a!![i]!! });
  }
  
  fun splitCSV(s : String) : Array&lt;String&gt; = forceNotNull(s.split(","));
  </code></pre>
  
  <p>However, now I'm getting a runtime error: </p>
  
  <blockquote>
    <p>ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String</p>
  </blockquote>
  
  <p>If I change T in <code>forceNotNull</code> to String, then it works, so I guess I'm close to a solution.</p>
  
  <p>Is this the right way to go about it? And if it is, how can I fix <code>forceNotNull</code> to work in the generic case?</p>