使用Spark,我最终得到了一些像这样的代码:
post("/auth/login", { req, res ->
val body = parseBody(req.body())
val assertion = body["assertion"]
if (assertion == null) {
halt(400)
return null
}
// ...snip...lots more code
})
Run Code Online (Sandbox Code Playgroud)
哪个很好用,除了......它不能编译 - 我明白了'return' is not allowed here.
我可以将lambda的剩余部分放在一个else块中,但我宁愿不想最大限度地减少压痕.
那么如何"短路"lambda以返回null?
在我的理解中,Lambda不能使用return,默认情况下返回的是函数.
所以你需要使用一个标签告诉return子句它将返回的位置.我用这个:
f@ {
...
return@f null
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1347 次 |
| 最近记录: |