Scala 单位类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3062804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:13:48  来源:igfitidea点击:

Scala Unit type

scalawhile-loop

提问by Don Ch

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

我使用opencsv来解析csv文件,我的代码是

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

I got a compiler warning saying:

我收到一个编译器警告说:

 comparing values of types Unit and Null using `!=' will always yield true
 [warn]     while( (aLine = reader.readNext()) != null ) {

How should I do the while loop?

我应该怎么做while循环?

采纳答案by Vasil Remeniuk

In your case (line = reader.readNext()) is a functional literal that returns type Unit. You may rewrite the code as follows:

在您的情况下 (line = reader.readNext()) 是一个返回类型 Unit 的函数式文字。您可以按如下方式重写代码:

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

回答by mkneissl

An assignment expression has type Unitin Scala. That is the reason for your compiler warning.

赋值表达式Unit在 Scala 中具有类型。这就是编译器警告的原因。

There is a nice idiom in Scala that avoids the while loop:

Scala 中有一个很好的习惯用法可以避免 while 循环:

val iterator = Iterator.continually(reader.readNext()).takeWhile(_ != null)

This gives you an iterator over whatever reader.readNextreturns.

这为您提供了任何reader.readNext返回的迭代器。

The continuallymethod returns an "infinite" iterator and takeWhiletakes the prefix of that, up to but not including the first null.

continually方法返回一个“无限”迭代器并takeWhile采用它的前缀,直到但不包括第一个空值。

(Scala 2.8)

(斯卡拉 2.8)

回答by oxbow_lakes

You can use a Streamto get what you want:

你可以使用 aStream来获得你想要的:

Stream.continually(reader.readLine()).takeWhile( _ ne null) foreach { line =>
  //do Stuff
}

This has the added advantage of other cool stuff as well:

这还有其他很酷的东西的额外优势:

Stream.continually(reader.readLine()).takeWhile( _ ne null) match {
  case head #:: tail => //perhaps you need to do stuff with the first element?
  case _             => //empty
}

EDIT- thanks to mkneissl for pointing out I should have included this warning:

编辑-感谢 mkneissl 指出我应该包含这个警告

scala> Stream.continually(1).take(100000000).foreach(x=>()) 

scala> val s = Stream.continually(1).take(100000000) 
s: scala.collection.immutable.Stream[Int] = Stream(1, ?) 

scala> s.foreach(x=>()) java.lang.OutOfMemoryError: Java heap space

回答by Jesper

You are writing Scala code they way you would write it in Java. Try doing it in a more Scala-like way. To read a text file line by line and do something with each line, try this:

您正在编写 Scala 代码,就像用 Java 编写的那样。尝试以更像 Scala 的方式来做。要逐行读取文本文件并对每一行执行某些操作,请尝试以下操作:

import java.io.File
import scala.io.Source

Source.fromFile(new File("myfile.txt")).getLines.foreach { line =>
    // Do something with the line, for example print it
    println(line)
}

回答by Don Mackenzie

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

Scala 中的赋值不返回值,Unit 类似于 C 或 C++ 中的 void。

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

这是有效的,因为返回了块中最后一个表达式的值,在这种情况下,它是while所需的布尔值