kotlin - 如果存在多个相同类型,如何返回特定的 lambda?(科特林)
问题描述
例如,当我有两个 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
}
}
解决方案
解决了!为尼克·李(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
}
}
推荐阅读
- java - Gradle如何添加VM选项以在javafx 11 idea上运行和部署jfoenix?
- sql - Oracle SQL 脚本给出错误。我尝试创建表
- mysql - 添加到字符串末尾的 SQL 命令
- forth - 在 Forth 中比较字符串
- qt - QML Text:如何使用 CSS 属性,例如 text-decoration:overline
- ethereum - 部署智能合约时出现节点错误 - Web3.js
- java - 如何为显式抛出的异常编写junit测试
- .net - 在 Visual Studio 2019 中无法获取异常的内部异常
- firebase - 如何配置 Flutter 应用以共享 Cloud Firestore
- grails - 如何为 Grails 域使用自定义模板?