来自lambda的Kotlin非本地返回作为扩展`Kotlin.jvm.functions.Function0'的接口传递` TL;博士
在Kotlin 1.3.61(或更新版本)中,是否有任何方法可以从作为函数参数的参数传递的lambda中生成非本地返回,该函数参数的类型是扩展了来自lambda的Kotlin非本地返回作为扩展`Kotlin.jvm.functions.Function0'的接口传递` TL;博士,kotlin,return,higher-order-functions,Kotlin,Return,Higher Order Functions,在Kotlin 1.3.61(或更新版本)中,是否有任何方法可以从作为函数参数的参数传递的lambda中生成非本地返回,该函数参数的类型是扩展了Kotlin.jvm.functions.Function0的接口 细节 在下面的Kotlin 1.3.61代码中,我在指定错误的注释之后的代码行中得到编译错误 如果返回值位于传递给接受扩展Function0接口的内联函数的lambda中,则看起来我无法从封闭函数生成非本地返回 但是,如果接收函数接受函数0(或()->单元)而不是子接口,则它可以工作 是
Kotlin.jvm.functions.Function0
的接口
细节
在下面的Kotlin 1.3.61代码中,我在指定错误的注释之后的代码行中得到编译错误
如果返回值位于传递给接受扩展Function0
接口的内联函数的lambda中,则看起来我无法从封闭函数生成非本地返回
但是,如果接收函数接受函数0
(或()->单元
)而不是子接口,则它可以工作
是否有任何方法可以通过扩展函数0
的接口使非本地返回工作
如果不是,这是Kotlin编译器中的错误,还是Kotlin尚不支持的功能
inline fun a(x: () -> Unit) {
x()
}
fun b(): Int {
a {return 1}
return 2
}
inline fun c(x: Function0<Unit>) {
x()
}
fun d(): Int {
c {return 3}
return 4
}
interface F: Function0<Unit>
inline fun e(x: F) {
x()
}
fun f(): Int {
// ERROR
// 'return' is not allowed here
// Type mismatch: inferred type is () -> [ERROR : Return not allowed] but F was expected
e {return 5}
return 6
}
fun g(): Int {
// ERRORS
// Interface F does not have constructors
// 'return' is not allowed here
e(F {return 7})
return 8
}
fun h(): Int {
// ERROR
// 'return' is not allowed here
e({return 9} as F)
return 0
}
println(b()) // properly prints 1 if code with errors is commented out
println(d()) // properly prints 3 if code with errors is commented out
println(f()) // should print 5
println(g()) // should print 7
println(h()) // should print 9
如果是后者,是否有任何现有的改进要求
如果不存在请求,这是Kotlin可以/应该支持的功能吗
inline fun a(x: () -> Unit) {
x()
}
fun b(): Int {
a {return 1}
return 2
}
inline fun c(x: Function0<Unit>) {
x()
}
fun d(): Int {
c {return 3}
return 4
}
interface F: Function0<Unit>
inline fun e(x: F) {
x()
}
fun f(): Int {
// ERROR
// 'return' is not allowed here
// Type mismatch: inferred type is () -> [ERROR : Return not allowed] but F was expected
e {return 5}
return 6
}
fun g(): Int {
// ERRORS
// Interface F does not have constructors
// 'return' is not allowed here
e(F {return 7})
return 8
}
fun h(): Int {
// ERROR
// 'return' is not allowed here
e({return 9} as F)
return 0
}
println(b()) // properly prints 1 if code with errors is commented out
println(d()) // properly prints 3 if code with errors is commented out
println(f()) // should print 5
println(g()) // should print 7
println(h()) // should print 9
a(x:()->单位){
x()
}
乐趣b():Int{
a{return 1}
返回2
}
内联趣味c(x:Function0){
x()
}
fund():Int{
c{return 3}
返回4
}
接口F:Function0
内联乐趣e(x:F){
x()
}
fun f():Int{
//错误
//此处不允许使用“return”
//类型不匹配:推断的类型为()->[错误:不允许返回]但应为F
e{return 5}
返回6
}
fung g():Int{
//错误
//接口F没有构造函数
//此处不允许使用“return”
e(F{return 7})
返回8
}
fun h():Int{
//错误
//此处不允许使用“return”
e({return 9}作为F)
返回0
}
println(b())//如果注释掉了有错误的代码,则正确打印1
println(d())//如果注释掉了有错误的代码,则正确打印3
println(f())//应该打印5
println(g())//应该打印7
println(h())//应该打印9
您调用了一个函数-这不应该是Int
?然后允许返回。当前,将整数返回到单位
。或者,由于您明确希望使用单位lambda,您是否尝试过return7@outer在外部函数上使用标记?Function0
是正确的,因为参数lambda中的返回不是lambda的本地返回;它是从封闭函数返回的非本地返回(例如,b()
,d()
,等等),d()
中没有错误,println(d())
正确地打印3,如果f()
,g()
,h()
(加上对它们的调用)被注释掉