importre / kotlin-unwrap

:gift: Unwrap nullable variables in Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

kotlin-unwrap

πŸŽπŸ“¦πŸ’πŸŽπŸ“¦πŸ’πŸŽπŸ“¦πŸ’

Download Build Status

Unwrap nullable variables

build.gradle

repositories {
    jcenter()
}

dependencies {
    compile "com.importre:kotlin-unwrap:<VERSION>"
}

Example

// returns nullable
fun foo(name: String?): String? = name

val _a = foo("Hello")
val _b = foo("World")
val _c = foo("!")

// example: unwrap all variables
unwrap(_a, _b, _c) { a, b, c ->
    println("$a, $b$c") // invoked
}

Error handling using otherwise

val _a = foo("Hello")
val _b = foo("World")
val _c = foo(null)

// example: error handing
unwrap(_a, _b, _c) { a, b, c ->
    println("$a, $b$c") // not invoked
} otherwise {
    println("Nah!")     // invoked because `_c` is null
}

Test

$ ./gradlew check

License

Apache 2.0 Β© Jaewe Heo

About

:gift: Unwrap nullable variables in Kotlin

License:Apache License 2.0


Languages

Language:Kotlin 81.1%Language:Python 18.9%