while
在 Java 中,我们通常可以在条件内执行赋值。然而,Kotlin 抱怨它。所以下面的代码无法编译:
val br = BufferedReader(InputStreamReader(
conn.inputStream))
var output: String
println("Output from Server .... \n")
while ((output = br.readLine()) != null) { // <--- error here: Assignments are not expressions, and only expressions are allowed in this context
println(output)
}
根据this other thread,这似乎是最好的解决方案:
val reader = BufferedReader(reader)
var line: String? = null;
while ({ line = reader.readLine(); line }() != null) { // <--- The IDE asks me to replace this line for while(true), what the...?
System.out.println(line);
}
但是是吗?