Scala Unit type

前端 未结 5 1384
情深已故
情深已故 2020-12-13 18:32

I use opencsv to parse csv files, and my code is

while( (line = reader.readNext()) != null ) { .... }

I got a compiler warning saying:

5条回答
  •  孤城傲影
    2020-12-13 19:02

    Assignment in Scala doesn't return a value, Unit is similar to void in C or C++.

    try

    var line = ""
    
    while ({line = reader.readNext(); line != null}) { ... }
    

    This works because the value of the last expression in a block is returned and in this case it is a Boolean which is required by the while

提交回复
热议问题