我正在尝试将Java类转换为Kotlin。 这是Java代码:
Observable.just("Stacey")
.zipWith(Observable.just(6),
(name, age) -> {
String text;
if(age < 18){
text = name + " is a child";
}else{
text = name + "is not a child";
}
return text;
}
);
这就是我将其转换为的内容:
Observable.just("Stacey")
.zipWith(Observable.just(6),
BiFunction<String, Int, String> {name, age ->
var text: String
if(age < 18){
text = "$name + is a child"
}else{
text = "$name + is not a child"
}
return text
}
)
Lambda表示法似乎根本不起作用,或者我只是无法弄清楚。我在Kotlin中找到的BiFunctions的所有示例都直接返回一个这样的值
BiFunction {姓名,年龄->名称+年龄}
这在语法上是正确的,但是在返回内容之前我需要一些其他逻辑。 出现两个错误消息:
此处不允许“返回”
类型不匹配。必需:单位,找到:字符串
但是我确实想返回一个字符串,并且我也明确声明了它。但是还有什么地方可以得到回报呢?
答案 0 :(得分:1)
我也遇到过这个问题,您要做的就是更换 使用返回文字 return @ BiFunction文字
有关说明,您可以在这里看看:
Kotlin: Whats does "return@" mean?
https://tutorialwing.com/labeled-return-or-return-in-kotlin-with-example