【问题标题】:How do I return a specific lambda if multiple of the same type are present? (KOTLIN)如果存在多个相同类型,如何返回特定的 lambda? (科特林)
【发布时间】:2019-12-22 07:39:04
【问题描述】:

例如,当我有两个 forEach lambda 时,我想返回一个特定的,例如以下示例中的 thing1,而不是 thing0。 如果我尝试以下操作,它会抱怨“在此范围内有多个标签具有这样的名称”

解释我的问题的伪代码:

thing0.forEach { _ ->
    thing1.forEach { _ ->
        // I'd like to return the thing1 forEach lambda, but the compiler
        // issues a warning that there are multiple forEach lambdas present.
        if (condition) return@forEach
    }
}

【问题讨论】:

标签: kotlin jetbrains-ide


【解决方案1】:

解决了!为尼克·李(Nick Lee)为我指明正确的方向而干杯! 原来我可以做到以下几点:

thing0.forEach { _ ->
    thing1.forEach nameHere@{ _ ->
        // I'd like to return the thing1 forEach lambda, but the compiler
        // issues a warning that there are multiple forEach lambdas present.
        if (condition) return@nameHere
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2020-06-18
    • 1970-01-01
    • 2022-11-19
    • 2021-05-11
    • 2021-07-15
    • 2011-01-15
    相关资源
    最近更新 更多