在以下代码:是否可以在Kotlin的while条件体中初始化一个变量?
var verticesCount: Int // to read a vertices count for graph
// Reading until we get a valid vertices count.
while (!Assertions.checkEnoughVertices(
verticesCount = consoleReader.readInt(null, Localization.getLocStr("type_int_vertices_count"))))
// The case when we don't have enough vertices.
println(String.format(Localization.getLocStr("no_enough_vertices_in_graph"),
Assertions.CONFIG_MIN_VERTICES_COUNT))
val resultGraph = Graph(verticesCount)
我们在最后一行获取下一个错误:
Error:(31, 33) Kotlin: Variable 'verticesCount' must be initialized
Assertions.checkEnoughVertices接受一个安全型变量作为自变量(verticesCount:智力),所以verticesCount不可能在这里被初始化或为null(并且我们在这些行上没有得到相应的错误)。
当已初始化的变量再次未初始化时,最后一行发生了什么?
'Assertions.checkEnoughVertices'是否有一个名为'verticesCount'的参数? – marstran
@marstran是的,它是这种方法的唯一参数 –