Назначение не допускается в то время как выражение?
В Java мы обычно можем выполнить назначение в пределахwhile
состояние. Однако Котлин жалуется на это. Таким образом, следующий код не компилируется:
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)
}
Согласно этому другомунитькажется, это лучшее решение:
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);
}
Но так ли это?