Git Product home page Git Product logo

kotlin-unwrap's Introduction

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

kotlin-unwrap's People

Contributors

importre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

myhau matrixy

kotlin-unwrap's Issues

Rename `nah` to an english word

In the syntax unwrap(x) {x -> } nah {} the nah function naming is not ideal.

I suppose NAH is an acronym for "null a.. h.."? Hm, I have no idea. Anyway, it would read better if it were a natural english word, like unwrap.. or ... Additionally, NAH very strongly resembles a Russian expletive for "go f*** yourself" :(

unwrapν–ˆμ„λ•Œ ν•˜λ‚˜λΌλ„ null일 κ²½μš°μ— νƒ€λŠ” λΈ”λŸ­

inline fun <T1, T2, R> unwrap(
        t1: T1?,
        t2: T2?,
        block: (T1, T2) -> R,
        block2: () -> R): R? {
    return if (t1 != null
            && t2 != null) {
        block(t1, t2)
    } else {
        block2()
    }

μ΄λŸ°μ‹μœΌλ‘œ νƒ”μœΌλ©΄ μ’‹κ² λ‹€ μ‹Άμ„λ•Œκ°€ μžˆμ—ˆλŠ”λ°, μ΄λ ‡κ²Œ λ§Œλ“€μ§€ μ•Šκ³  ν•  수 μžˆλŠ” 방법이 μžˆμ„κΉŒμš”?

Refactor code to make Func class more readable

I think in Func.kt file you can check variables in more elegant way:

    val hasNull = arrayOf<Any?>(t1, t2).contains(null)
    if (!hasNull) block(t1, t2)
    return Unwrap(valid = !hasNull)

Instead of

    return Unwrap().apply {
        if (t1 != null
                && t2 != null) {
            valid = true
            block(t1, t2)
        }
    }

To do it you should make valid field as constructor parameter. It make your code more readable and maintanable and also allow explicitly specify valid field that is more preferable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.