# What is the reason behind Smart Cast being impossible to perform when referenced class is in another module?

**URL:** <https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201>\
**Category:** Language Design\
**Created:** [January 5, 2017, 8:52am UTC](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201 "2017-01-05T08:52:02Z")\
**Posts on this page:** 7\
**Page:** 3

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [August 8, 2021, 9:10pm UTC](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201/42 "2021-08-08T21:10:44Z")

</div>

“trust” other modules to do what? That each and every ‘val’ property returns always the same object?

---

<div class="post-metadata">

**Author:** ![alamothe](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@alamothe](https://discuss.kotlinlang.org/u/alamothe)\
**Post date:** [August 9, 2021, 4:25pm UTC](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201/43 "2021-08-09T16:25:43Z")

</div>

Basically it would mean that those modules are always built together. If there is a change in one module, the other module is always built.

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [August 9, 2021, 4:57pm UTC](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201/44 "2021-08-09T16:57:14Z")

</div>

If you expect code to be all compiled at the same time you shouldn’t use modules in the first place.

If you read the documentation for modules  
[https://kotlinlang.org/spec/packages-and-imports.html#modules](https://kotlinlang.org/spec/packages-and-imports.html#modules)  
it says:

> Kotlin module is a set of Kotlin files which are considered to be interdependent and must be handled together during compilation.

---

<div class="post-metadata">

**Author:** ![kapaseker](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kapaseker/32/6449_2.png) [@kapaseker](https://discuss.kotlinlang.org/u/kapaseker)\
**Post date:** [November 17, 2021, 10:29am UTC](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201/45 "2021-11-17T10:29:20Z")

</div>

```auto
val foo: String = j.foo

```

to

```auto
j.foo?.let{val fool:String = it}

```

---

<div class="post-metadata">

**Author:** ![rotbolt](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@rotbolt](https://discuss.kotlinlang.org/u/rotbolt)\
**Post date:** [April 12, 2023, 4:47am UTC](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201/46 "2023-04-12T04:47:58Z")

</div>

I do agree with this statement of @yole

Although quite late here to add to this conversation

> A smart cast is only valid when multiple accesses of the same property are guaranteed to return the same value. If the property being accessed is defined in a different module from the access location, the module containing the property can be recompiled separately from the module where it’s accessed, breaking the key requirement of the smart cast

I think if someone still want to enforce the smart casting to some degree accross modules, they can use kotlin contracts  
For example, I wanted smart casting for non-nullability. For this I simply wrote contract for a function like this

```auto
fun <T>enforceNonNullSmartCast(data: T?): T? {
    contract {
        returnsNotNull() implies (data != null)
    }
    return data
}

```

At call site

```auto
        val safeData = enforceNonNullSmartCast(data)
        if(safeData == null) return
        // use smart cast non-null data now

```

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [April 12, 2023, 6:14am UTC](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201/47 "2023-04-12T06:14:18Z")

</div>

Is this any different than `val safeData = data`?

---

<div class="post-metadata">

**Author:** ![rotbolt](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@rotbolt](https://discuss.kotlinlang.org/u/rotbolt)\
**Post date:** [April 12, 2023, 7:18am UTC](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201/48 "2023-04-12T07:18:39Z")

</div>

Actually no, right after posting this message, it realized I can simply use `val safeData = data` for non-nullability smart case

[Previous page](https://discuss.kotlinlang.org/t/what-is-the-reason-behind-smart-cast-being-impossible-to-perform-when-referenced-class-is-in-another-module/2201.md?page=2)
