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

前端 未结 5 739
自闭症患者
自闭症患者 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:46

    In case of generic classes casts cannot be checked because type information is erased in runtime. But you check that all objects in the list are Waypoints so you can just suppress the warning with @Suppress("UNCHECKED_CAST").

    To avoid such warnings you have to pass a List of objects convertible to Waypoint. When you're using * but trying to access this list as a typed list you'll always need a cast and this cast will be unchecked.

提交回复
热议问题