# Default value for undefined case

**URL:** <https://discuss.kotlinlang.org/t/default-value-for-undefined-case/16226>\
**Category:** Language Design\
**Created:** [February 5, 2020, 4:31pm UTC](https://discuss.kotlinlang.org/t/default-value-for-undefined-case/16226 "2020-02-05T16:31:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Void](https://avatars.discourse-cdn.com/v4/letter/v/8baadc/32.png) [@Void](https://discuss.kotlinlang.org/u/Void)\
**Post date:** [February 5, 2020, 4:31pm UTC](https://discuss.kotlinlang.org/t/default-value-for-undefined-case/16226/1 "2020-02-05T16:31:17Z")

</div>

For enums it’s common to have default/undefined value like:

```kotlin
data class User(val gender: Gender = Gender.Undefined)

```

And then it’s clear for a developer that enum has list of options and there is `Undefined` one:

```kotlin
if(user.gender == Gender.Undefined) doIfUndefined()
else doIfDefined()

```

But for classes like `Date` it doesn’t work because there is no any hint for a developer that there is undefined case. It is still possible to defined undefined date instance:

```kotlin
object UndefinedDate : Date(-1L)

fun Date.isUndefined(): Boolean {
    return this == UndefinedDate
}

class User(val birth: Date = UndefinedDate)

```

But it’s error prone because developers might use just -1 date as it is.

Is there something better than making such field nullable? Or maybe there is some future language feature which informs developers about default/undefined value?

---

<div class="post-metadata">

**Author:** ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)\
**Post date:** [February 5, 2020, 4:44pm UTC](https://discuss.kotlinlang.org/t/default-value-for-undefined-case/16226/2 "2020-02-05T16:44:09Z")

</div>

Just because I’m curious, why does this needs to be undefined instead of absent (null)?  
Especially when there are so much features in a language to deal with null?

I asume you have three options to specify undefined:

- wrap the type: Kotlin does not provide [Union-types](https://discuss.kotlinlang.org/t/union-types/77) yet, but the second best is [sealed classes](https://kotlinlang.org/docs/reference/sealed-classes.html).

- Change the type: add a specific field which tells if the type is present. Or use sealed classes again. You need to change the class for this, for both cases, every type needs to know about it, so it must be important…

- Use a specific value for the type: use a specific instance ([null-object pattern](https://www.geeksforgeeks.org/null-object-design-pattern/)) or set the value itself to something that doesn’t make sense for the type (eg. -1 for age).  
Then you can add an extension function which checks if it is defined

---

<div class="post-metadata">

**Author:** ![dfriehs](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@dfriehs](https://discuss.kotlinlang.org/u/dfriehs)\
**Post date:** [February 6, 2020, 7:33am UTC](https://discuss.kotlinlang.org/t/default-value-for-undefined-case/16226/3 "2020-02-06T07:33:18Z")

</div>

Just to add to the sealed class example, it is possible to reduce the wrapper and use extension functions with contracts to add smart casts for simple `if` statements like this:

```
sealed class Container<out T> {
    object Undefined : Container<Nothing>()
    class Defined<T>(val value: T) : Container<T>()
}

@ExperimentalContracts
fun <T> Container<T>.isDefined(): Boolean {
    contract {
        returns(true) implies (this@isDefined is Container.Defined<T>)
    }

    return this is Container.Defined<T>
}

@ExperimentalContracts
fun main() {
    val x: Container<Int> = Container.Undefined

    if (x.isDefined()) {
        println(x.value)
    }
}

```

---

<div class="post-metadata">

**Author:** ![mice777](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mice777/32/4560_2.png) [@mice777](https://discuss.kotlinlang.org/u/mice777)\
**Post date:** [February 11, 2020, 8:23am UTC](https://discuss.kotlinlang.org/t/default-value-for-undefined-case/16226/4 "2020-02-11T08:23:36Z")

</div>

One way is to create instance of “undefined” object, and use this instance everywhere as undefined value. Then you’d check for [referential equality](https://kotlinlang.org/docs/reference/equality.html#referential-equality) with `===` operator.

```auto
class C{
    companion object{
        val UNDEFINED_DATE = Date(0)
    }

    fun workOnDate(date: Date) {
        if(date===UNDEFINED_DATE)
            throw IllegalStateException()
    }
}

```

Another way is to pass nullable value into a function, and filter out null values at beginning, working with non-null value for the rest of function:

```auto
    fun workOnDate(inDate: Date?) {
        val date = inDate?:throw IllegalStateException()
    }

```
