# Smart cast issue: if-else and when

**URL:** <https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698>\
**Category:** Support\
**Created:** [December 6, 2017, 10:44am UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698 "2017-12-06T10:44:52Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [December 6, 2017, 10:44am UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698/1 "2017-12-06T10:44:52Z")

</div>

I found some issues with smart cast, here two examples:

```
fun exampleElse(vararg args: String) {
    val something: Any = TODO()
    if (something is String) throw Exception()
    else // <---
        if (something !is Long) throw Exception()
    something.dec()
}

fun exampleWhen(vararg args: String) {
    val something: Any = TODO()
    when (something) {
        !is Long -> throw Exception()
    }
    something.dec()
}

```

In the first example it is possible to invoke `dec()` but, when `else` is not commented, the IntelliJ’s auto completition fails.

In the second case smart cast fails indifferently.

Edit: Kotlin version 1.2

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [December 6, 2017, 2:01pm UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698/2 "2017-12-06T14:01:36Z")

</div>

The comment probably changes whether the if is independent or not. Without the comment it will be part, and analysis of multiple if statements may be incomplete (in actuality the analysis is quite tricky.

For the second one, it is clear that this is also not analysed completely.

---

<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:** [December 6, 2017, 4:31pm UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698/3 "2017-12-06T16:31:07Z")

</div>

The first example compiles always, but autocomplete works sometimes.

---

<div class="post-metadata">

**Author:** ![mrtact](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mrtact/32/4751_2.png) [@mrtact](https://discuss.kotlinlang.org/u/mrtact)\
**Post date:** [December 6, 2017, 5:48pm UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698/4 "2017-12-06T17:48:07Z")

</div>

I have noticed that `when` doesn’t seem to lend itself to (admittedly, complex) smart casts. For example, this doesn’t work:

```kotlin
val args = arrayOf(Any)
when {
   args.isEmpty() -> throw IllegalArgumentException("Empty args")
   args[0] !is Foo -> throw IllegalArgumentException("Invalid arg type")
   else -> args[0].doFooThing()
}

```

---

<div class="post-metadata">

**Author:** ![Xerus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/xerus/32/4499_2.png) [@Xerus](https://discuss.kotlinlang.org/u/Xerus)\
**Post date:** [December 6, 2017, 10:27pm UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698/5 "2017-12-06T22:27:47Z")

</div>

May that be an issue of it being an array rather than an issue of when?

---

<div class="post-metadata">

**Author:** ![mrtact](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mrtact/32/4751_2.png) [@mrtact](https://discuss.kotlinlang.org/u/mrtact)\
**Post date:** [December 8, 2017, 2:32pm UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698/6 "2017-12-08T14:32:14Z")

</div>

Could be.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [December 8, 2017, 2:38pm UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698/7 "2017-12-08T14:38:27Z")

</div>

Arrays are mutable. There is no guarantee that multiple accesses of `args[0]` will return the same value.

---

<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:** [December 14, 2017, 2:04pm UTC](https://discuss.kotlinlang.org/t/smart-cast-issue-if-else-and-when/5698/8 "2017-12-14T14:04:21Z")

</div>

Missing autocomplete for example ‘exampleElse’ using Kotlin 1.2.10, too.
