# Rationale behind avoiding Smart cast for mutable properties

**URL:** <https://discuss.kotlinlang.org/t/rationale-behind-avoiding-smart-cast-for-mutable-properties/13437>\
**Category:** Language Design\
**Created:** [July 10, 2019, 8:53am UTC](https://discuss.kotlinlang.org/t/rationale-behind-avoiding-smart-cast-for-mutable-properties/13437 "2019-07-10T08:53:03Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [July 13, 2019, 2:42pm UTC](https://discuss.kotlinlang.org/t/rationale-behind-avoiding-smart-cast-for-mutable-properties/13437/4 "2019-07-13T14:42:34Z")

</div>

Just wanted to add some of the runnable (and editable) examples for fun (taken from this post: [Nullable property lazy allocation - #36 by arocnies](https://discuss.kotlinlang.org/t/nullable-property-lazy-allocation/8283/36)).

```run-kotlin
fun main(args: Array<String>) {
//sampleStart
    foo = true

    if (foo != null) {
        println("foo: $foo")
    }
//sampleEnd
}

var foo: Boolean? = true 
    get() = field.also {field = null}

```

```run-kotlin
fun main(args: Array<String>) {
//sampleStart
    foo = true

    if (foo != null) {
        println("foo: $foo")
        println("foo: $foo")
        println("foo: $foo")
    }
//sampleEnd
}

var foo: Boolean? = true 
    get() = when(field) {
        true -> { field = false; true }
        false -> { field = null; false }
        null -> { field = true; null }
    }

```

```run-kotlin
fun main(args: Array<String>) {
//sampleStart
    if (foo != null) {
        doSomething()
        print("foo: $foo")
    }
//sampleEnd
}

var foo: Boolean? = true

fun doSomething() {
    // doSomething ...
    foo = null
}

```

---

_[View the full topic](https://discuss.kotlinlang.org/t/rationale-behind-avoiding-smart-cast-for-mutable-properties/13437)._
