是任何人都面临这个问题.
break or continue jump across class boundary kotlin
当我打算使用break或continue时会出现此问题.里面的lambda与接收器我创建'letIn'
lambda与接收器代码
fun letIn(componentName: String?, values: List<LifeService.Value?>?,
body: (String, List<LifeService.Value?>) -> Unit) {
if (!TextUtils.isEmpty(componentName) && (values != null && values.isNotEmpty())) {
body(componentName!!, values)
}
}
Run Code Online (Sandbox Code Playgroud)
这个示例代码.
for (option in 0 until optionsSize) {
val component = optionsGroup?.options?.get(option)
component?.let {
with(component) {
letIn(presentation, values, { componentName, values ->
if (componentName == LifeComponentViewType.CHECKBOX) {
letIn(transformCheckBoxValues(optionsGroup), { data ->
dataSource?.push(componentName, ComponentDataCheckBoxCollection(name, data))
view.buildComponent(componentName)
// break or continue didnt work
})
} else {
dataSource?.push(componentName, ComponentDataCollection(name, values))
view.buildComponent(componentName)
}
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
因为上面的代码没有用,所以我使用命令式的方式.
for (option in 0 until optionsSize) {
val component = optionsGroup?.options?.get(option)
if (component != null) {
val presentation: String? = component.presentation
val values = component.values
if (!TextUtils.isEmpty(presentation)) {
if (presentation == LifeComponentViewType.CHECKBOX) {
val data = transformCheckBoxValues(optionsGroup)
if (data.isNotEmpty()) {
dataSource?.push(presentation, ComponentDataCheckBoxCollection(optionsGroup.name, data))
view.buildComponent(presentation)
return
}
} else {
dataSource?.push(presentation!!, ComponentDataCollection(component.name, values))
view.buildComponent(presentation!!)
}
} else {
return
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人有建议?
更新 我已经通过内联高阶函数解决了这个问题.
(抛开其他编码错误)您看到错误,因为在lambda中,您不能使用break或continue跳出lambda到最近的循环.相反,您可以使用限定符return跳出lambda到标签.
参考语言参考
return-expression从最近的封闭函数返回,即foo.(请注意,仅对传递给内联函数的lambda表达式支持此类非本地返回.)如果我们需要从lambda表达式返回,我们必须标记它并限定返回:
(强调我的)
您的第二个示例显示您希望lambda从封闭函数执行非本地返回.因此,您不需要对您的资格进行限定return,但letIn必须声明您的函数inline(否则您只能进行本地的合格返回).
inline fun letIn(componentName: String?, values: List<LifeService.Value?>?,
body: (String, List<LifeService.Value?>) -> Unit) {
if (!TextUtils.isEmpty(componentName) && (values != null && values.isNotEmpty())) {
body(componentName!!, values)
}
}
Run Code Online (Sandbox Code Playgroud)
......或者如果你想让它有接收器......
inline fun String?.letIn(values: List<LifeService.Value?>?,
body: String.(List<LifeService.Value?>) -> Unit) {
if (!TextUtils.isEmpty(this) && (values != null && values.isNotEmpty())) {
this!!.body(values)
}
}
Run Code Online (Sandbox Code Playgroud)
当你声明letIn为inline,那么你可以放在return你的lambda中而不需要编译器抱怨.inline如果你的lambdas只做本地返回,你的函数就不需要了,但它需要有一个合格的返回(例如return@letIn).
你的第一个例子看起来像这样......
for (option in 0 until optionsSize) {
val component = optionsGroup?.options?.get(option)
component?.let {
with(component) {
presentation.letIn(values, { values ->
if (this == LifeComponentViewType.CHECKBOX) {
this.letIn(transformCheckBoxValues(optionsGroup), { data ->
dataSource?.push(this, ComponentDataCheckBoxCollection(this, data))
view.buildComponent(this)
return //returns from function
})
} else {
dataSource?.push(this, ComponentDataCollection(name, values))
view.buildComponent(this)
return //returns from function
}
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后,请注意,如果您想提前跳出lambda,但继续外循环,如:
fun test1() {
val list = listOf("a", "b", "c")
val optionsSize = 2
for(i in 0..optionsSize) loop@ {
println("calliing list.forEach")
list.forEach lit@ {
if(it == "a") return@lit
if(it == "c") return@loop
println(it)
}
}
}
Run Code Online (Sandbox Code Playgroud)
它不会起作用.Intelli-sense不会抱怨它,但编译器会抛出内部错误.但你可以将外部循环转换为lambda,它确实有效......
fun test() {
val list = listOf("a", "b", "c")
val optionsSize = 2
(0..optionsSize).forEach() loop@ {
println("calliing list.forEach")
list.forEach lit@ {
if(it == "a") return@lit
if(it == "c") return@loop
println(it)
}
}
}
Run Code Online (Sandbox Code Playgroud)
同样,这仅在声明传递lambda的函数时才有效inline(如forEach声明的那样inline).
| 归档时间: |
|
| 查看次数: |
1907 次 |
| 最近记录: |