The question may sound silly, but there is no typo in it.
fun test(): Any {
return return true
}
This is actually possible in Kotlin. Although the compiler warns about
不可达代码
为外部返回.但这只是一个警告.
I don't want to compare Java with Kotlin, but I was interested whether the same would work in Java.
public class Test {
// ...
static int test() {
return return 1;
}
}
It does not!
/Test.java:8: error: illegal start of expression
return return 1;
^
/Test.java:8: error: not a statement
return return 1;
^
2 errors
为什么Kotlin 是这样设计的?