Kotlin: How to work with List casts: Unchecked Cast: kotlin.collections.List<Kotlin.Any?> to kotlin.colletions.List<Waypoint>
<p>I want to write a function that returns every item in a <code>List</code> that is not the first or the last item (a via point). The function gets a generic <code>List&lt;*&gt;</code> as input. A result should only be returned if the elements of the list are of the type <code>Waypoint</code>:</p>

<pre><code>fun getViaPoints(list: List&lt;*&gt;): List&lt;Waypoint&gt;? {

    list.forEach { if(it !is Waypoint ) return null }

    val waypointList = list as? List&lt;Waypoint&gt; ?: return null

    return waypointList.filter{ waypointList.indexOf(it) != 0 &amp;&amp; waypointList.indexOf(it) != waypointList.lastIndex}
}
</code></pre>

<p>When casting the <code>List&lt;*&gt;</code> to <code>List&lt;Waypoint&gt;</code>, I get the warning: </p>

<blockquote>
  <p>Unchecked Cast: kotlin.collections.List
  to kotlin.colletions.List</p>
</blockquote>

<p>I can't figure out a way to implement it otherwise. What's the right way to implement this function without this warning?</p>