r

    rrader

    3 years ago
    is there a function analogical to
    let
    but for empty strings
    someString?.let { calculate(it) }
    I need
    someString.doIfNotEmpty { calculate(it) }
    ?
    Czar

    Czar

    3 years ago
    If I have to do that often, I usually define one like this:
    private inline fun String?.doIfNotEmpty(block: (String) -> Unit) {
    	this?.takeIf(String::isNotEmpty)?.let { block(it) }
    }
    Note, I'm treating
    null
    as empty here, if your usage is different, you may have to account for that.
    kirillrakhman

    kirillrakhman

    3 years ago
    ?.let(block)
    😉