# Smart cast on collection

**URL:** <https://discuss.kotlinlang.org/t/smart-cast-on-collection/6895>\
**Category:** Language Design\
**Created:** [March 6, 2018, 5:02am UTC](https://discuss.kotlinlang.org/t/smart-cast-on-collection/6895 "2018-03-06T05:02:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aug828](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@aug828](https://discuss.kotlinlang.org/u/aug828)\
**Post date:** [March 6, 2018, 5:02am UTC](https://discuss.kotlinlang.org/t/smart-cast-on-collection/6895/1 "2018-03-06T05:02:57Z")

</div>

How to make the following code more concise?

```
nodes : List<Int>? = listOf(1, 2, 3)
if (nodes != null) nodes[0] else null

```

Something like the following would be great, but it doesn’t seem to be supported right now.  
`nodes?[0]`

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [March 6, 2018, 5:50am UTC](https://discuss.kotlinlang.org/t/smart-cast-on-collection/6895/2 "2018-03-06T05:50:54Z")

</div>

```
nodes?.get(0)

```

---

<div class="post-metadata">

**Author:** ![aug828](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@aug828](https://discuss.kotlinlang.org/u/aug828)\
**Post date:** [March 6, 2018, 5:58am UTC](https://discuss.kotlinlang.org/t/smart-cast-on-collection/6895/3 "2018-03-06T05:58:46Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [March 6, 2018, 8:42am UTC](https://discuss.kotlinlang.org/t/smart-cast-on-collection/6895/4 "2018-03-06T08:42:53Z")

</div>

[http://kotlinlang.org/docs/reference/operator-overloading.html](http://kotlinlang.org/docs/reference/operator-overloading.html)

---

<div class="post-metadata">

**Author:** ![aug828](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@aug828](https://discuss.kotlinlang.org/u/aug828)\
**Post date:** [March 6, 2018, 5:20pm UTC](https://discuss.kotlinlang.org/t/smart-cast-on-collection/6895/5 "2018-03-06T17:20:30Z")

</div>

With the operator overloading, we can actually do this:

```
operator fun<T> List<T>?.get(index : Int): T? {
    return this?.get(index)
}
val nodes: List<Int>? = listOf(1, 2, 3)
nodes[0]

```

I don’t know whether it’s good or bad. This is more concise. But I think there must be a reason why it’s not in the standard library.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 6, 2018, 5:57pm UTC](https://discuss.kotlinlang.org/t/smart-cast-on-collection/6895/6 "2018-03-06T17:57:03Z")

</div>

I think the reason is that you would no longer see that `nodes` can be null at this point. It would just make it harder for someone reading the code to understand it. And how often would you actually use this? The only time I can think of is if you just want to access the first element and than you can just write `nodes?.get(0)`.  
It’s not like you can try to iterate it as you no longer now the length of the list 🙂
