concept optional datum in category kotlin

appears as: optional data
The Joy of Kotlin

This is an excerpt from Manning's book The Joy of Kotlin.

Representing optional data has always been a problem in computer programs. The concept of optional data is simple. In everyday life, it’s easy to represent the absence of something when this something is stored in a container—whatever it is, it can be represented by an empty container. For example, an absence of apples can be represented by an empty apple basket. The absence of gasoline in a car can be visualized as an empty gas tank. But representing the absence of data in computer programs is a little more difficult.

Listing 7.9 The ToonMail application handling optional data correctly
fun main(args: Array<String>) {

    val toons: Map<String, Toon>  = mapOf(
        "Mickey" to Toon("Mickey", "Mouse", "mickey@disney.com"),
        "Minnie" to Toon("Minnie", "Mouse"),
        "Donald" to Toon("Donald", "Duck", "donald@disney.com"))

    val toon = getName()
        .flatMap(toons::getResult)
        .flatMap(Toon::email)

    println(toon)

}

fun getName(): Result<String> = try {
    validate(readLine())
} catch (e: IOException) {
    Result.failure(e)
}

fun validate(name: String?): Result<String> = when {
    name?.isNotEmpty() ?: false -> Result(name)
    else -> Result.failure(IOException())    #1  
}

#1   The validate function is modified to simulate an IOException when no name is input.