# Smart cast not possible for lazy

**URL:** <https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420>\
**Category:** Uncategorized\
**Created:** [February 2, 2019, 11:52am UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420 "2019-02-02T11:52:56Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [February 2, 2019, 11:52am UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/1 "2019-02-02T11:52:56Z")

</div>

Recently, I’ve had a huge annoyance: I wanted to use a lazy value and smart cast it. A lazy value computes its result once and then always returns the same value, so I thought it could surely be smart-casted. Turns out, it can’t. Why?  
Is there simply no possibility for the compiler to express such a thing? I’d rather not use `let` blocks or local variables everytime I access that lazy, since I might as well use a var if I have to check it all the time anyways.

The problem why I can’t do it differently is that I am writing an android application and I have a variable that has to be initialized in onCreate. As such it can’t be a simple `val`, since it is not initialized in the constructor, yet it does not change its value ever after creation. I opted for lazy since that also implies it to be a `val`, but Kotlin refuses to treat it as “real” `val`.

---

<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:** [February 2, 2019, 11:57am UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/2 "2019-02-02T11:57:24Z")

</div>

I don’t really understand your problem. Can you give us a short example?

---

<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:** [February 2, 2019, 12:02pm UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/3 "2019-02-02T12:02:51Z")

</div>

This is a pretty random example, but it illustrates the point:

```auto
sealed class Value {
	data class IntValue(val int: Int): Value()
	data class StringValue(val string: String): Value()
}

val value: Value by lazy {
	if(System.currentTimeMillis() % 2 == 1L)
		Value.IntValue(1)
	else
		Value.StringValue("test")
}

fun main() {
	if(value is Value.IntValue) {
		println(value.int) // Compilation error: Smartcast is impossible because property has open or custom getter
	}
}

```

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [February 2, 2019, 12:08pm UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/4 "2019-02-02T12:08:25Z")

</div>

In order to make smart cast here, you need to create specialized intrinsic for `lazy` delegate. In general, delegated value could change therefore no smart casts. For your particular example you can just write:

```kotlin
val _value = value
if(_value is Value.IntValue) {
	println(_value.int)
}

```

---

<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:** [February 2, 2019, 10:03pm UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/5 "2019-02-02T22:03:24Z")

</div>

Would this be something contracts could deal with?

I don’t have experience with contracts so I may be off here. Is there a contract for purity or, promising to return the same value, that could be made to solve all cases where a `Smartcast is impossible because property has open or custom getter`?

---

<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:** [February 2, 2019, 10:07pm UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/6 "2019-02-02T22:07:23Z")

</div>

Currently this can’t be solved with contracts. It might be possible once contracts can be used for class members, but even then we would need some kind of purity contract as you say.

---

<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:** [February 3, 2019, 8:54am UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/7 "2019-02-03T08:54:33Z")

</div>

Yep, that works for that particular case. I’ll expand my example to come closer to my real-world problem:

```auto
sealed class Value {
	data class IntValue(val int: Int): Value()
	data class DoubleValue(val double: Double): Value()
	data class StringValue(val string: String): Value()
}

val value: Value by lazy {
	if(System.currentTimeMillis() % 2 == 1L)
		Value.IntValue(1)
	else
		Value.StringValue("test")
}

fun main() {
	when(value) {
		is Value.IntValue -> println(value.int)
		is Value.DoubleValue -> println(value.double)
		is Value.StringValue -> println(value.string)
	}
}

```

I know, I can use a local variable here as well. But what if every time I use this variable I have such a when statement (which I do in my Android project that uses the MVI model)? I do not want to declare a local variable every single time, neither use a `let` block or something that increases nesting even more.

I think arocnies is right, this should probably be solved with contracts.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [February 3, 2019, 8:56am UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/8 "2019-02-03T08:56:21Z")

</div>

> [@Xerus](#):
>
> I think arocnies is right, this should probably be solved with contracts.

Probably it could. But for now, it is not possible since `lazy{}` is just regular delegate-generating function and not a language construct. You can redefine it

---

<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:** [February 3, 2019, 9:20am UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/9 "2019-02-03T09:20:38Z")

</div>

“You can redefine it”

What do you mean with that? What use could there be in redefining it?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [February 3, 2019, 9:23am UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/10 "2019-02-03T09:23:05Z")

</div>

I mean that it is library function, you can in theory create a function with the same name in different package and use it. So the only way to make smart cast is to add intrinsic for this specific function. It does not make sense. Of course contracts are made just to solve that.

---

<div class="post-metadata">

**Author:** ![mtopolnik](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mtopolnik/32/5616_2.png) [@mtopolnik](https://discuss.kotlinlang.org/u/mtopolnik)\
**Post date:** [February 7, 2019, 8:09am UTC](https://discuss.kotlinlang.org/t/smart-cast-not-possible-for-lazy/11420/11 "2019-02-07T08:09:24Z")

</div>

Since Kotlin 1.3 you can write this, which avoids leaking the local var to the outside scope:

```auto
when(val _value = value) {
    is Value.IntValue -> println(_value.int)
    is Value.DoubleValue -> println(_value.double)
    is Value.StringValue -> println(_value.string)
}

```
