[kotlin] Kotlin의 다중 변수 렛

kotlin에서 여러 nullable 변수에 대해 여러 let을 연결하는 방법이 있습니까?

fun example(first: String?, second: String?) {
    first?.let {
        second?.let {
            // Do something just if both are != null
        }
    }
}

내 말은, 다음과 같습니다.

fun example(first: String?, second: String?) {
    first?.let && second?.let {
        // Do something just if both are != null
    }
}



답변

여기에 관심이 있다면 이것을 해결하는 두 가지 기능이 있습니다.

inline fun <T: Any> guardLet(vararg elements: T?, closure: () -> Nothing): List<T> {
    return if (elements.all { it != null }) {
        elements.filterNotNull()
    } else {
        closure()
    }
}

inline fun <T: Any> ifLet(vararg elements: T?, closure: (List<T>) -> Unit) {
    if (elements.all { it != null }) {
        closure(elements.filterNotNull())
    }
}

용법:


// Will print
val (first, second, third) = guardLet("Hello", 3, Thing("Hello")) { return }
println(first)
println(second)
println(third)

// Will return
val (first, second, third) = guardLet("Hello", null, Thing("Hello")) { return }
println(first)
println(second)
println(third)

// Will print
ifLet("Hello", "A", 9) {
 (first, second, third) ->
 println(first)
 println(second)
 println(third)
}

// Won't print
ifLet("Hello", 9, null) {
 (first, second, third) ->
 println(first)
 println(second)
 println(third)
}


답변

다음은 사용하려는 스타일, 모든 유형이 같거나 다른 경우, 목록에 알 수없는 항목 수에 따라 몇 가지 변형이 있습니다.

혼합 유형, 새 값을 계산하려면 모두 null이 아니어야합니다.

혼합 유형의 경우 어리석게 보일 수 있지만 혼합 유형에 대해 잘 작동하는 각 매개 변수 수에 대해 일련의 함수를 빌드 할 수 있습니다.

inline fun <T1: Any, T2: Any, R: Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2)->R?): R? {
    return if (p1 != null && p2 != null) block(p1, p2) else null
}
inline fun <T1: Any, T2: Any, T3: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, block: (T1, T2, T3)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null) block(p1, p2, p3) else null
}
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, block: (T1, T2, T3, T4)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null && p4 != null) block(p1, p2, p3, p4) else null
}
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, T5: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, p5: T5?, block: (T1, T2, T3, T4, T5)->R?): R? {
    return if (p1 != null && p2 != null && p3 != null && p4 != null && p5 != null) block(p1, p2, p3, p4, p5) else null
}
// ...keep going up to the parameter count you care about

사용 예 :

val risk = safeLet(person.name, person.age) { name, age ->
  // do something
}   

목록에 null 항목이 없을 때 코드 블록 실행

여기서 두 가지 특징은 목록에 null이 아닌 항목이 모두있을 때 코드 블록을 실행하는 것이고, 두 번째는 목록에 null이 아닌 항목이 하나 이상있을 때 동일한 작업을 수행하는 것입니다. 두 경우 모두 null이 아닌 항목 목록을 코드 블록에 전달합니다.

기능 :

fun <T: Any, R: Any> Collection<T?>.whenAllNotNull(block: (List<T>)->R) {
    if (this.all { it != null }) {
        block(this.filterNotNull()) // or do unsafe cast to non null collectino
    }
}

fun <T: Any, R: Any> Collection<T?>.whenAnyNotNull(block: (List<T>)->R) {
    if (this.any { it != null }) {
        block(this.filterNotNull())
    }
}

사용 예 :

listOf("something", "else", "matters").whenAllNotNull {
    println(it.joinToString(" "))
} // output "something else matters"

listOf("something", null, "matters").whenAllNotNull {
    println(it.joinToString(" "))
} // no output

listOf("something", null, "matters").whenAnyNotNull {
    println(it.joinToString(" "))
} // output "something matters"

함수가 항목 목록을 받고 동일한 작업을 수행하도록 약간 변경했습니다.

fun <T: Any, R: Any> whenAllNotNull(vararg options: T?, block: (List<T>)->R) {
    if (options.all { it != null }) {
        block(options.filterNotNull()) // or do unsafe cast to non null collection
    }
}

fun <T: Any, R: Any> whenAnyNotNull(vararg options: T?, block: (List<T>)->R) {
    if (options.any { it != null }) {
        block(options.filterNotNull())
    }
}

사용 예 :

whenAllNotNull("something", "else", "matters") {
    println(it.joinToString(" "))
} // output "something else matters"

이러한 변형은 다음과 같은 반환 값을 갖도록 변경할 수 있습니다. let() .

null이 아닌 첫 번째 항목 사용 (Coalesce)

SQL Coalesce 함수와 유사하게 null이 아닌 첫 번째 항목을 반환합니다. 기능의 두 가지 특징 :

fun <T: Any> coalesce(vararg options: T?): T? = options.firstOrNull { it != null }
fun <T: Any> Collection<T?>.coalesce(): T? = this.firstOrNull { it != null }

사용 예 :

coalesce(null, "something", null, "matters")?.let {
    it.length
} // result is 9, length of "something"

listOf(null, "something", null, "matters").coalesce()?.let {
    it.length
}  // result is 9, length of "something"

기타 변형

… 다른 변형이 있지만 사양이 더 많으면 범위를 좁힐 수 있습니다.


답변

이를 위해 자신의 함수를 작성할 수 있습니다.

 fun <T, U, R> Pair<T?, U?>.biLet(body: (T, U) -> R): R? {
     val first = first
     val second = second
     if (first != null && second != null) {
         return body(first, second)
     }
     return null
 }

 (first to second).biLet { first, second ->
      // body
 }


답변

arrayIfNoNulls함수 를 만들 수 있습니다 .

fun <T : Any> arrayIfNoNulls(vararg elements: T?): Array<T>? {
    if (null in elements) {
        return null
    }
    @Suppress("UNCHECKED_CAST")
    return elements as Array<T>
}

그런 다음 다음을 사용하여 가변 개수의 값에 사용할 수 있습니다 let.

fun example(first: String?, second: String?) {
    arrayIfNoNulls(first, second)?.let { (first, second) ->
        // Do something if each element is not null
    }
}

이미 배열이있는 경우 takeIfNoNulls함수를 만들 수 있습니다 ( takeIf및에서 영감을 얻음 requireNoNulls) :

fun <T : Any> Array<T?>.takeIfNoNulls(): Array<T>? {
    if (null in this) {
        return null
    }
    @Suppress("UNCHECKED_CAST")
    return this as Array<T>
}

예:

array?.takeIfNoNulls()?.let { (first, second) ->
    // Do something if each element is not null
}


답변

두 값만 확인하고 목록 작업을 수행 할 필요가없는 경우 :

fun <T1, T2> ifNotNull(value1: T1?, value2: T2?, bothNotNull: (T1, T2) -> (Unit)) {
    if (value1 != null && value2 != null) {
        bothNotNull(value1, value2)
    }
}

사용 예 :

var firstString: String?
var secondString: String?
ifNotNull(firstString, secondString) { first, second -> Log.d(TAG, "$first, $second") }


답변

사실, 간단하게 할 수 있습니다. 😉

if (first != null && second != null) {
    // your logic here...
}

Kotlin에서 정상적인 null 검사를 사용하는 데 아무런 문제가 없습니다.

또한 코드를 살펴 보는 모든 사람이 훨씬 더 읽기 쉽습니다.


답변

실제로 다음 도우미 함수를 사용하여 해결하는 것을 선호합니다.

fun <A, B> T(tuple: Pair<A?, B?>): Pair<A, B>? =
    if(tuple.first == null || tuple.second == null) null
    else Pair(tuple.first!!, tuple.second!!)

fun <A, B, C> T(tuple: Triple<A?, B?, C?>): Triple<A, B, C>? =
    if(tuple.first == null || tuple.second == null || tuple.third == null) null
    else Triple(tuple.first!!, tuple.second!!, tuple.third!!)


fun <A, B> T(first: A?, second: B?): Pair<A, B>? =
    if(first == null || second == null) null
    else Pair(first, second)

fun <A, B, C> T(first: A?, second: B?, third: C?): Triple<A, B, C>? =
        if(first == null || second == null || third == null) null
        else Triple(first, second, third)

사용 방법은 다음과 같습니다.

val a: A? = someValue
val b: B? = someOtherValue
T(a, b)?.let { (a, b) ->
  // Shadowed a and b are of type a: A and b: B
  val c: C? = anotherValue
  T(a, b, c)
}?.let { (a, b, c) ->
  // Shadowed a, b and c are of type a: A, b: B and c: C
  .
  .
  .
}