Kotlin: How to work with List casts: Unchecked Cast: kotlin.collections.List

前端 未结 5 734
自闭症患者
自闭症患者 2020-12-07 10:51

I want to write a function that returns every item in a List that is not the first or the last item (a via point). The function gets a generic List<*&g

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 11:28

    To improve @hotkey's answer here's my solution:

    val waypointList = list.filterIsInstance().takeIf { it.size == list.size }
    

    This gives you the List if all the items can be casted, null otherwise.

提交回复
热议问题