# How do you handle undefined on dynamic value

**URL:** <https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247>\
**Category:** JavaScript\
**Created:** [August 29, 2018, 8:22am UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247 "2018-08-29T08:22:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [August 29, 2018, 8:22am UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/1 "2018-08-29T08:22:15Z")

</div>

When programming in javascript the following snippet is very common:

```
var foo = someApi.someFoo()
if (foo) foo.bar()

```

When a javascript API can return all kind of values (an object, null or undefined), then this is the shortest way in javascript for checking for an actual object.

The direct translation in kotlin would be:

```
val foo = someApi.someFoo()
if (foo != null && foo != undefined) foo.bar

```

This is very cumbersome. Another pain point is that the elvis operator and the safe call operator is obviously not working for undefined.

How do you best handle undefined in kotlin?

---

<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:** [August 29, 2018, 8:28am UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/2 "2018-08-29T08:28:25Z")

</div>

```kotlin
val <T> T?.nullable : T?
  get() = when(this){
    null -> null
    undefined -> null
    else -> this
  }

```

I do something like this for optionals.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [August 29, 2018, 9:06am UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/3 "2018-08-29T09:06:36Z")

</div>

Will that work also, if the compile-time type of your nullable value is dynamic? Example:

```
val c: dynamic = someApi.someFoo()
c.nullable // what does this invoke?

```

Does this actually invoke the extension property, or does it invoke the dynamic object’s dynamic property?

---

<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:** [August 29, 2018, 9:10am UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/4 "2018-08-29T09:10:46Z")

</div>

I’ve never checked, but it should. You invoke a property on any object and check if the instance equals `null` or `undefined`.

the first `?` in my example is probably not needed.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [August 29, 2018, 3:05pm UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/5 "2018-08-29T15:05:19Z")

</div>

When I import the extension function and try to use it on a dynamic value, Intellij will show a warning that the import is not used. It makes somewhat sense that it would not use the statically dispatched extension since there could be a dynamically dispatched member on the dynamic type. In worst case the dynamic member would be undefined which would yield an undefined value.

I guess I will have to test your idea to see whether it works on dynamic values.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [August 30, 2018, 8:13am UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/6 "2018-08-30T08:13:17Z")

</div>

> [@fatjoe79](#):
>
> it would not use the statically dispatched extension since there could be a dynamically dispatched member on the dynamic type […] I guess I will have to test your idea to see whether it works on dynamic values.

My assumption was right. I made the following test:

```
val <T> T?.nullable : String
  get() = when(this){
    null -> "works: was null"
    undefined -> "works: was undefined"
    else -> "works: was instance"
  }

fun test() {
  console.log("test start")

  val foo1: dynamic = Foo
  val foo2: Foo = Foo
  val bar1: dynamic = Bar
  val bar2: Bar = Bar

  console.log("foo1.nullable is: ${foo1.nullable}")
  console.log("foo2.nullable is: ${foo2.nullable}")
  console.log("bar1.nullable is: ${bar1.nullable}")
  console.log("bar2.nullable is: ${bar2.nullable}")
}

object Foo
object Bar{ val nullable = "barMember" }

```

And it prints this to the console:

```
test start
foo1.nullable is: undefined
foo2.nullable is: works: was instance
bar1.nullable is: barMember
bar2.nullable is: barMember

```

As can be seen with foo1, the extension function is not used, when the value’s compile-time type is dynamic. The tests foo1 and bar1 together show that it will try to invoke the dynamic value’s own member (without knowing whether it exists).

Makes sense to me. But does not solve my question. How should one best deal with undefined values on dynamic types?

---

<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:** [August 30, 2018, 12:21pm UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/7 "2018-08-30T12:21:25Z")

</div>

I never really used Kotlin JS but can you declare the extension for dynamic types?

```kotlin
val dynamic.nullable
    get() = ...

```

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [August 30, 2018, 12:46pm UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/8 "2018-08-30T12:46:40Z")

</div>

No, the compiler does not allow declaring extensions for dynamic type. Probably because it cannot be invoked (as described in my previous post).

---

<div class="post-metadata">

**Author:** ![Svyatoslav.Kuzmich](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svyatoslav.kuzmich/32/6748_2.png) [@Svyatoslav.Kuzmich](https://discuss.kotlinlang.org/u/Svyatoslav.Kuzmich)\
**Post date:** [October 2, 2018, 9:32am UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/9 "2018-10-02T09:32:47Z")

</div>

`dynamic` type works pretty much like JavaScript

Kotlin code for `dynamic` variable `foo`:

```auto
if (foo) foo.bar

```

will be translated to JavaScript as:

```auto
if (foo) foo.bar;

```

* * *

Also, you don’t have to do the same check for null and undefined separately because `null == undefined` in Kotlin and JS

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [October 2, 2018, 9:47am UTC](https://discuss.kotlinlang.org/t/how-do-you-handle-undefined-on-dynamic-value/9247/10 "2018-10-02T09:47:17Z")

</div>

> [@Svyatoslav.Kuzmich](#):
>
> Kotlin code for `dynamic` variable `foo` :
> 
> ```auto
> if (foo) foo.bar
> 
> ```
> 
> will be translated to JavaScript as:
> 
> ```auto
> if (foo) foo.bar;
> 
> ```

Maybe I missed this in documentation, or maybe it is not documented because it seems obvious. I was hoping it works like this. Good to know for sure, thanks.
